合并GridView中某列相同信息的行(单元格),如图:
解决方法如下(写在公共模块里,定义为静态方法方便调用):
1 /// <summary>
2 /// 合并GridView中某列相同信息的行(单元格)
3 /// </summary>
4 /// <param name="GridView1">GridView</param>
5 /// <param name="cellNum">第几列</param>
6 public static void GroupRows(GridView GridView1, int cellNum)
7 {
8 int i = 0, rowSpanNum = 1;
9 while (i < GridView1.Rows.Count - 1)
10 {
11 GridViewRow gvr = GridView1.Rows[i];
12
13 for (++i; i < GridView1.Rows.Count; i++)
14 {
15 GridViewRow gvrNext = GridView1.Rows[i];
16 if (gvr.Cells[cellNum].Text == gvrNext.Cells[cellNum].Text)
17 {
18 gvrNext.Cells[cellNum].Visible = false;
19 rowSpanNum++;
20 }
21 else
22 {
23 gvr.Cells[cellNum].RowSpan = rowSpanNum;
24 rowSpanNum = 1;
25 break;
26 }
27
28 if (i == GridView1.Rows.Count - 1)
29 {
30 gvr.Cells[cellNum].RowSpan = rowSpanNum;
31 }
32 }
33 }
34 }
35
36
如果多列要求合并时,可作适当修改。