限制 GridView 列中的文本大小 [英] Limit text size in GridView column
问题描述
我有一个 asp:GridView 声明如下:
I have an asp:GridView declared as follows:
<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>
我的问题是 100% 审美.长描述的自动换行使表格看起来很俗气.我想要对长描述做的是在描述太长时有一个省略号 (...)
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
详细描述等等……
我找不到为此的内置方法,所以我决定尝试实现 GridView 的这个 OnRowDataBound
.
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) + "...";
}
我在第一行收到运行时异常,因为未将对象引用设置为对象的实例.
I get an run time exception on the first line because of Object reference not set to an instance of an object.
我在这里做错了什么?有没有更简单的方法来完成我想要做的事情?
What am I doing wrong here? Is there a simpler way to accomplish what I'm trying to do?
推荐答案
您可以使用 css 处理它,并将其添加到您的 Grid
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屋!