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)+"...";
           }
       }        
            
      
   }

}
按照以上就能够!
相关文章
相关标签/搜索