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?
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" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With