如何在 Grid View 列标题上鼠标悬停时添加工具提示

9
当用户悬停在 GridView 中某个列的列标题上时,例如:列标题年份,当我悬停在“年份”上时,应该看到该年份意义的解释,“这是学生入读大学的年份等。”
以下是我的 ascx 代码:
 <asp:GridView ID="grdView" runat="server" Width="900px" AutoGenerateColumns="False"
                AllowPaging="true" AllowSorting="true" CellSpacing="0" CellPadding="5" PageSize="20"
        OnRowDataBound="grdView_RowDataBound">
                <Columns>
 <asp:TemplateField HeaderText="ID Number" ItemStyle-Width="90px" >
    <ItemTemplate>
      <asp:Label ID="Label1" runat="server" Text='<%# Bind("ID")%'></asp:Label>
    </ItemTemplate>
 </asp:TemplateField><asp:BoundField DataField="StudentName" HeaderText="StudentName"> </asp:BoundField>

请告诉我如何在我的网格视图的列标题上悬停文本或工具提示。

谢谢,
6个回答

9

Here is the CSS tooltip for Gridview in C# using Jquery

protected void grd_popup_details_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        for (int i = 0; i < e.Row.Cells.Count; i++)
        {
            e.Row.Cells[i].ToolTip = e.Row.Cells[i].Text;
        }
    }

Refference link


除了信息图表之外,我最想加一点! :) 你怎么做到的? - Fandango68
如何仅为特定列添加工具提示,比如在您的答案中仅为“评论”添加。 - Vishal I P
错误的解决方案!OP要求在GridView列标题上鼠标悬停时添加工具提示,而不是在每个单元格内添加。 - jbrekke

7
我从未进行过任何ASP.NET开发,但似乎在此提供了一种解决方案:如何在ASP.NET中为每个表头列添加标题 您的示例可能如下所示:
 <asp:GridView ID="grdView" runat="server" Width="900px" AutoGenerateColumns="False"
            AllowPaging="true" AllowSorting="true" CellSpacing="0" CellPadding="5" PageSize="20"
    OnRowDataBound="grdView_RowDataBound">
            <Columns>
 <asp:TemplateField HeaderText="ID Number" ItemStyle-Width="90px" >
<HeaderTemplate>
       <asp:Label ID="Header" ToolTip="HERE WE GO!!!!" runat="server" Text="Label"></asp:Label>
       </HeaderTemplate>
    <ItemTemplate>
      <asp:Label ID="Label1" runat="server" Text='<%# Bind("ID")%'></asp:Label>
    </ItemTemplate>
 </asp:TemplateField><asp:BoundField DataField="StudentName" HeaderText="StudentName"> </asp:BoundField>

我会尝试一下 :)

7
在您的代码后台,创建GridView的rowDataBound方法并添加以下代码。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        foreach (TableCell cell in e.Row.Cells)
        {
            cell.Attributes.Add("title", "Tooltip text for " + cell.Text);
        }
    }
}

不要忘记在GridView中设置OnRowDataBound属性。

http://rosshawkins.net/archive/2007/04/15/adding-tooltips-to-gridview-headers.html.aspx


单元格是这样的,那标题呢? - SearchForKnowledge
crs0794发布的链接是一个很好的为标题添加工具提示的方法。值得一读。 - Doreen

2
if (e.Row.RowType == DataControlRowType.DataRow)
{
     e.Row.Cells[1].ToolTip = Grd.Columns[1].HeaderText;
}

1
感谢您提供这段代码片段,它可能会提供一些有限的、即时的帮助。通过展示为什么这是一个好的解决方案,一个适当的解释将极大地提高它的长期价值,并使它对未来有其他类似问题的读者更加有用。请[编辑]您的答案,添加一些解释,包括您所做出的假设。 - Suraj Rao

1
这里有一个例子,展示了即使在Autogenerate=True且GridView列是动态确定的情况下,也可以使用ColumnName。此外,当文本包含转义字符(如双引号)时,似乎需要使用HtmlDecode()。
Dictionary<string, int> _headerIndiciesForDetailsReportGridView = null;

protected void detailsReportGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (_headerIndiciesForDetailsReportGridView == null)
    {
        int index = 0;
        _headerIndiciesForDetailsReportGridView = ((Table)((GridView)sender).Controls[0]).Rows[0].Cells
            .Cast<TableCell>()
            .ToDictionary(c => c.Text, c => index++);
    }

    if (e.Row.RowType == DataControlRowType.DataRow)
    {                            
        TableCell cell = e.Row.Controls[_headerIndiciesForDetailsReportGridView["theColumnName"]] as TableCell;

        // Shorten text in a particular column to a max of 20 characters.
        // Set tooltip to the full original text. Decode to remove &quot, etc.
        //
        string orgText = cell.ToolTip = HttpUtility.HtmlDecode(cell.Text);

        if (orgText.Length > 20)    // If cell text should be shortened
            cell.Text = HttpUtility.HtmlEncode(orgText.Substring(0, 20) + "...");
    }
}

0
 <ItemTemplate>                                                                               <asp:HiddenField ID="HiddenField1" runat="server" Value='<%#Bind("EmpInfoDetail") %>' />                                                                                                                                                              <asp:Label ID="Label3" runat="server" Text="" Visible="false" Font-Bold="true" ForeColor="#cc3300" CssClass="tooltip"></asp:Label>                                                                                                                                                                                                                                                                                         </ItemTemplate> 
    


     use this code on grid view row data bound   
        #region show grid text on mouse hover
                        if (e.Row.RowType == DataControlRowType.DataRow)
                        {
                            DataRowView drv = e.Row.DataItem as DataRowView;
                            Label test = e.Row.FindControl("Label3") as Label;
                            if (drv["EmpInfoDetail"].ToString().Length > 500)
                            {
                                test.Text = drv["EmpInfoDetail"].ToString().Substring(0, 500) + "...";
                            }
                            else
                            {
                                test.Text = drv["EmpInfoDetail"].ToString();
                            }
            
                            e.Row.Cells[1].ToolTip = drv["EmpInfoDetail"].ToString();
            
                        }
                        #endregion

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接