如何在网格视图列标题上添加鼠标悬停工具提示

时间:2012-11-08 21:23:14

标签: asp.net gridview

当用户将鼠标悬停在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>

请让我知道如何将鼠标悬停在我的gridview列标题上的文本或工具提示上。 谢谢,

5 个答案:

答案 0 :(得分:7)

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

答案 1 :(得分:5)

在你的代码中,为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

答案 2 :(得分: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) + "...");
    }
}

答案 3 :(得分:1)

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

答案 4 :(得分:0)

 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