2012-11-08 28 views
5

當用戶將鼠標懸停在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

我從來沒有做過任何asp.net開發,但似乎有一個解決方案此處提供:how to add title for every header column in gridview in 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> 

我會給一個嘗試:)

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

+0

這樣做的單元格,頭怎麼樣? – SearchForKnowledge

+0

發佈的鏈接crs0794是將工具提示添加到標題的絕佳方式。值得一讀。 – Doreen

6

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

+0

加1爲信息圖形比什麼都重要! :) 你是怎樣做的? – Fandango68

+0

如何僅爲特定列添加工具提示,在您的答案中僅說明評論 –

0

它可能使用的ColumnName的,即使自動生成= True和GridView的列進行動態確定。

當文本包含諸如雙引號之類的轉義字符時,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; 

     // 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) + "..."); 
    } 
} 
相關問題