我有一个动态GridView1,AutogenerateFields设置为TRUE。以下代码将在GridView中显示单元格的值,但我需要做的是在将鼠标悬停在单元格上时显示列名称。任何帮助表示赞赏。谢谢!
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;
}
}
}
答案 0 :(得分:2)
使用此代码段。您可以循环OnRowDataBound
事件中的所有单元格,并从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;
}
}
}
答案 1 :(得分:1)
这很简单,我想知道你为什么要在你的列名为headertext.bdw时显示工具提示你去。
<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>