guan yu pai ke xi tong de ruan jian
源代码在线查看: 合并gridview中某列相同信息的行(单元格).txt
1 ///
2 /// 合并GridView中某列相同信息的行(单元格)
3 ///
4 /// GridView
5 /// 第几列
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
1 合并多列#region 合并多列
2 /**////
3 /// GridView合并
4 ///
5 /// GridView
6 /// 起始列Index
7 /// 结束列Index
8 public static void MergeGridViewRows(GridView gdv, int startColumnIndex, int endColumnIndex)
9 {
10 if (gdv == null || endColumnIndex < startColumnIndex || gdv.Rows.Count < 2)
11 return;
12 if (startColumnIndex < 0 || endColumnIndex > gdv.Columns.Count - 1)
13 throw new ArgumentOutOfRangeException("列Index超出GridView可用列的范围。");
14 EndColumnIndex = endColumnIndex;
15 MergeCellWithSubColumn(gdv, 0, 0, gdv.Rows.Count - 1);
16 }
17 private static int EndColumnIndex = 0;
18
19 /**////
20 /// 合并当前列和后续列
21 ///
22 /// 当前列
23 /// 起始行
24 /// 结束行
25 ///
26 private static void MergeCellWithSubColumn(GridView gdv, int currentColumnIndex, int startRowIndex, int endRowIndex)
27 {
28 if (currentColumnIndex > EndColumnIndex)//结束递归
29 return;
30 string preValue = GetCellValue(gdv,startRowIndex, currentColumnIndex);
31 string curValue = string.Empty;
32 int endIndex = startRowIndex;
33 for (int i = startRowIndex + 1; i 34 {
35 if (i == endRowIndex + 1)
36 curValue = null;//完成最后一次合并
37 else
38 curValue = GetCellValue(gdv, i, currentColumnIndex);
39 if (curValue != preValue)
40 {
41 //合并当前列
42 MergeColumnCell(gdv, currentColumnIndex, endIndex, i - 1);
43 //合并后续列
44 MergeCellWithSubColumn(gdv, currentColumnIndex + 1, endIndex, i - 1);
45 endIndex = i;
46 preValue = curValue;
47 }
48 }
49 }
50