Home About Us SQL Interview Book Contact Us RSS
Articles
Tools
Code Snippets
Links
Tips & Tricks
FAQ
Resources
Articles
Code Snippets
Links
FAQ
Resources

Saravana Kumar
Vinod Kumar


ToolTip for DataGrid Header 

In this code snippet, you will see how to  set the tool tip for DataGrid Header. If you have headertemplate for DataGrid, then it is very simple to set the tooltip for header. You can directly set tool tip for each colum in headertemplate. But if dont have headertemplate in your DataGrid, i.e. headers are automatically generated by DataGrid. There is not direct way to set the tooltip, in this article i will explain how to do this.

There are two ways to set the tool tip for datagrid header. One way is by writing event handler for ItemCreated event and look for header item creation. Then access the header item, after that you can set the tool tip for each item. Here is the code snippet for this method,

Private Sub DataGrid1_ItemCreated(ByVal sender As Object, ByVal e As 
                      System.Web.UI.WebControls.DataGridItemEventArgs)  Handles DataGrid1.ItemCreated
 
    If e.Item.ItemType = ListItemType.Header Then
            e.item.cells(0).Tooltip = "Name"
            e.item.cells(1).Tooltip = "Age"
    end if

End Sub

Other way is by accessing the control collection of datagrid. In this method, you can directly access the DataGrid Header control in control collection and then set the tool tip for each column. This way is good at performance but it not fully foolproof. So other way in this method is, looping through each control in the control collection and then look for header itemtype. If the item is header item, then set the tool tip for each column. Here is the code snippet for both the ways, 

Method1

ctype(DataGrid1.Controls(0).Controls(1),DataGriditem).Cells(0).ToolTip="Mytooltip1"

Method2

Dim oControl As ControlFor Each oControl In DataGrid1.Controls(0).Controls
 
    If CType(oControl, DataGridItem).ItemType = ListItemType.Header Then
        CType(oControl, DataGridItem).Cells(0).ToolTip = "Mytooltip1"
    End If
 
Next

You can write this code anywhere after you bind your datagrid. i.e once the controls are created(after itemcreated event ) in DataGrid. You can access the controls collection and set the tool tip.