在GridView中列的限制文字大小 [英] Limit text size in GridView column
问题描述
我有一个 ASP:GridView的声明如下:
<asp:GridView runat="server" id="dg_myprojects" AllowSorting="true" AutoGenerateColumns="false" Width="900px" CssClass="Grid" OnSorting="TaskGridView_SortingMine" OnRowCommand="MyProjectList_RowCommand" DataKeyNames="project_id" OnRowDataBound="Ds_my_projects_RowDataBound">
<AlternatingRowStyle CssClass="alternateRow" />
<HeaderStyle CssClass="GridHeader" />
<Columns>
<asp:BoundField DataField="project_name" HeaderText="Project Name" SortExpression="project_name"/>
<asp:BoundField DataField="description" HeaderText="Description" SortExpression="description" ItemStyle-HorizontalAlign="Left" />
<asp:BoundField DataField="role" HeaderText="Role" SortExpression="role" />
<asp:BoundField DataField="start_date" HeaderText="Start Date" SortExpression="start_date" DataFormatString="{0:d}"/>
<asp:BoundField DataField="end_date" HeaderText="End Date" SortExpression="end_date" DataFormatString="{0:d}" />
<asp:BoundField DataField="client" HeaderText="Client" SortExpression="client" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="DeleteButton" CommandArgument='<%# Eval("project_id") %>' CommandName="Remove" runat="server">Remove</asp:LinkButton>
</ItemTemplate></asp:TemplateField>
<asp:HyperLinkField DataNavigateUrlFields="project_id" DataNavigateUrlFormatString="EditProject.aspx?pID={0}" Text="Edit"/>
</Columns>
</asp:GridView>
我的问题IS100%的审美。出现这种情况长期描述的文字换行使该表显得俗气。我想用很长的描述做的是有一个省略号(...)时的描述变得很长。
My problem is100% aesthetic. The word wrap that happens for long descriptions make the table look tacky. What I want to do with a long description is have an ellipses (...) when the description gets too long
长描述等等等等...
Long description blah blah...
我找不到一个内置的这种方法,所以我决定尝试实现这个 OnRowDataBound
在GridView的。
I couldn't find a built in method for this so I decided to try to implement this OnRowDataBound
of the GridView.
protected void Ds_my_projects_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRow curRow = ((DataRowView)e.Row.DataItem).Row;
if (curRow["Description"].ToString().Length > 200)
curRow["Description"] = curRow["Description"].ToString().Substring(0, 200) + "...";
}
我得到,因为不设置到对象的实例
我在做什么错在这里?是否有一个更简单的完成我想要做的方式是什么?
What am I doing wrong here? Is there a simpler way to accomplish what I'm trying to do?
推荐答案
您可以用CSS,并添加以下内容到电网
CSS类处理它
You could handle it with css and by adding this to your Grid
css class:
.Grid {
table-layout:fixed;
width:100%;
}
.Grid .Shorter {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
更新:我修改了上面的类,这样就可以通过ItemStyle-的CssClass属性,像这样影响个人列:
Update: I modified the above class so that you can affect an individual column by using the ItemStyle-CssClass attribute like so:
<asp:BoundField DataField="description" HeaderText="Description"
SortExpression="description" ItemStyle-CssClass="Shorter" />
这篇关于在GridView中列的限制文字大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!