GridView中的每一個單元格文本長度的控制,鼠標懸停時,顯示單元格全部的內容

怎樣實現GridView中的每一個單元格文本長度的控制,鼠標懸停時,顯示單元格全部的內容,方法如下:
1)在.aspx頁面GridView控件添加OnDataBound屬性,以下所示:
<asp:GridView ID="GridView1" runat="server" OnDataBound="GridView1_DataBound"></asp:GridViw>
2)在後臺.aspx.cs文件GridView1_DataBound事件函數添加如下代碼:
c#

private void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
   //斷定當前類型是否爲數據行,若是是,則添加title
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
       //獲取列數,進行循環添加title
       for(int i=0;i<e.Row.Cells.Count;i++)
       {
           //定義一個string類型變量用來存放每一個單元格的內容
           string temp = e.Row.Cells[i].text;
           //設置title爲GridView的HeadText
           e.Row.Cells[i].Attributes.Add("title",temp);//未截取長度    
           //斷定temp的長度,
           if(temp.length>10)
           {
             //截取字符串
             temp = temp.SubString(0,9)+"...";
           }
       }        
            
      
   }

}
按照以上就能夠!
相關文章
相關標籤/搜索