Reputation: 65
I have a dynamic GridView1 with AutogenerateFields set to TRUE. The following code will display the value of the cell in a GridView but what I need for it to do is to display the column name when you hover over a cell. Any help is appreciated. Thanks!
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
for (int i = 0; i < GridView1.Columns.Count; i++)
{
e.Row.Cells[i].ToolTip = GridView1.Columns[i].HeaderText;
}
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
foreach (TableCell gvcell in e.Row.Cells)
{
gvcell.ToolTip = gvcell.Text;
}
}
}
Upvotes: 0
Views: 1983
Reputation: 35514
Use this snippet. You can loop all the cell on the OnRowDataBound
event and get the header text from the HeaderRow
.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//check if the row is a datarow
if (e.Row.RowType == DataControlRowType.DataRow)
{
//loop all cells in the row
for (int i = 0; i < e.Row.Cells.Count; i++)
{
//set the tooltip text to be the header text
e.Row.Cells[i].ToolTip = GridView1.HeaderRow.Cells[i].Text;
}
}
}
Upvotes: 2
Reputation: 169
Thats pretty simple , i wonder why u want to show tooltip when u have column name as headertext.bdw here u go.
<asp:TemplateField HeaderText="Category">
<ItemTemplate>
<asp:Label runat="server" ToolTip="Category" ID="lblCategory" Text='<%#Eval("Category") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sub Category">
<ItemTemplate>
<asp:Label runat="server" ToolTip="SubCategory" ID="lblSubCategory" Text='<%#Eval("SubCategory") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Upvotes: 1