Sunday, April 20, 2008

ASP.NET GridView Tips - ToolTip

If you can't display fully the columns from a database table, you can display the remaining columns in tooltips.

Here is an example where a field named 'Description' is shown truncated to 20 characters in the gridview and at the same time the field value will be displayed fully in a tooltip for the corresponding cell in Description column.

Following is the code for use in the Frontend /markup.



<asp:TemplateField HeaderText="Description"
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Left(Eval("Description"),20) %>'
tooltip='<%# Eval("Description") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>



This can be done using Codebehind from the RowDataBound event of the GridView also as follows (VB.NET) - assuming that column number 6 is 'Description' column.



Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

If e.Row.RowType = DataControlRowType.DataRow Then

e.Row.Cells(5).Text = Left(e.Row.DataItem("Description").ToString, 20)
e.Row.Cells(5).ToolTip = e.Row.DataItem("Description").ToString
End If
End Sub




As posted in Microsoft ASP.NET Forum

No comments: