Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 758
Rondel Avatar asked Oct 04 '11 16:10

Rondel


1 Answers

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" />
like image 133
CAbbott Avatar answered Oct 17 '22 17:10

CAbbott