http://www.cnblogs.com/nianming/archive/2013/08/19/3268981.html
1: /// <summary>
2: /// 和并列
3: /// </summary>
4: /// <param name="gv">要合并的GridView</param>
5: /// <param name="startCol">开始列的索引</param>
6: /// <param name="endCol">结束列的索引</param>
7: /// <param name="containHeader">是否合并表头,默认不合并</param>
8: public static void MergeColumn(GridView gv, int startCol, int endCol, bool containHeader = false)
9: {
10: if (containHeader)
11: {
12: BLRowCells(gv.HeaderRow, startCol, endCol);
13: }
14: foreach (GridViewRow row in gv.Rows)
15: {
16: BLRowCells(row, startCol, endCol);
17: }
18: }
19:
20: /// <summary>
21: /// 遍历GridViewRow中的单元格
22: /// </summary>
23: /// <param name="row">要遍历的行</param>
24: /// <param name="start">开始索引</param>
25: /// <param name="end">结束索引</param>
26: private static void BLRowCells(GridViewRow row, int start, int end)
27: {
28: //从开始索引的下一列开始
29: for (int i = start + 1; i <= end; i++)
30: {
31: //当前单元格
32: TableCell currCell = row.Cells[i];
33: //前一个单元格
34: TableCell prevCell = row.Cells[i - 1];
35: if (!string.IsNullOrEmpty(currCell.Text) && !string.IsNullOrEmpty(prevCell.Text))
36: {
37: if (currCell.Text == prevCell.Text)
38: {
39: currCell.ColumnSpan = prevCell.ColumnSpan < 1 ? 2 : prevCell.ColumnSpan + 1;
40: prevCell.Visible = false;
41: }
42: }
43: }
44: }
作者:田念明 出处:http://www.cnblogs.com/nianming/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。