Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

c# - Limit text size in GridView column

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>

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...

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;        
}

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" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...