简体   繁体   中英

Display the name of a column in a gridview cell tooltip ASP.net/C#

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;

        }
    }
}

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;
        }
    }
}

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM