怎样实现GridView中的每个单元格文本长度的控制,鼠标悬停时,显示单元格所有的内容,方法以下:
1)在.aspx页面GridView控件添加OnDataBound属性,如下所示:
<asp:GridView ID="GridView1" runat="server" OnDataBound="GridView1_DataBound"></asp:GridViw>
2)在后台.aspx.cs文件 GridView1_DataBound事件函数添加以下代码:
1)在.aspx页面GridView控件添加OnDataBound属性,如下所示:
<asp:GridView ID="GridView1" runat="server" OnDataBound="GridView1_DataBound"></asp:GridViw>
2)在后台.aspx.cs文件 GridView1_DataBound事件函数添加以下代码:
01 | private void GridView1_RowDataBound( object sender, GridViewRowEventArgs e) |
02 | { |
03 | //判定当前类型是否为数据行,如果是,则添加title |
04 | if (e.Row.RowType == DataControlRowType.DataRow) |
05 | { |
06 | //获取列数,进行循环添加title |
07 | for ( int i=0;i<e.Row.Cells.Count;i++) |
08 | { |
09 | //定义一个string类型变量用来存放每个单元格的内容 |
10 | string temp = e.Row.Cells[i].text; |
11 | //设置title为GridView的HeadText |
12 | e.Row.Cells[i].Attributes.Add( "title" ,temp); //未截取长度 |
13 | //判定temp的长度, |
14 | if (temp.length>10) |
15 | { |
16 | //截取字符串 |
17 | temp = temp.SubString(0,9)+ "..." ; |
18 | } |
19 | } |
20 | |
21 | |
22 | } |
23 |
24 | } |