Tooltip for GridView Column Headers (ASP.Net)
Posted on 30. Mar, 2009 by khader in Other
I just wanted to add auto tooltips for my GridView headers. There are couple of ways but I don’t want to use javascript and wanted to use simple ‘title’ attribute. It took couple of hours to get the working code supporting sortable columns. I thought it will be very useful for others. Here’s the code and logic behind it.
I am using RowDataBound event of the GridView to add tooltip manually. So I created a event handler. In the handler I checking the type of column header whether it is simple Text or a Link to sort the data. The reason is I cannot retrieve the name of the Column if is clickable header. Here is the code (VBScript):
'Assume your grid control name - GVDashBoard
Protected Sub GVDashBoard_RowDataBound(ByVal sender As Object,
ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
Handles GVDashBoard.RowDataBound
Dim titleSet As Boolean = False
If e.Row.RowType = DataControlRowType.Header Then
For Each cell As TableCell In e.Row.Cells
For Each ctl As Control In cell.Controls
If ctl.GetType().ToString().Contains("DataControlLinkButton") Then
cell.Attributes.Add("title", "Sort by - " + CType(ctl,LinkButton).Text)
titleSet = True
End If
Next
If Not titleSet Then
cell.Attributes.Add("title", "Tooltip for Column - " + cell.Text)
End If
titleSet = False
Next
End If
End Sub
If you are looking to add a tooltip manually you can do shown below:
Kavi
15. Jun, 2009
Pretty useful!
Rani
15. Jun, 2009
Handy code. In C# as well please…
amol
19. Feb, 2010
thank u…
Emily
01. Jun, 2010
Handy code. In C# as well please…