【ASP.NET】 Gridview合并单元格
View Code
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI.WebControls; 6 /// <summary> 7 ///GridviewProcess 处理Griedview合并 8 /// </summary> 9 public class GridviewProcess 10 { 11 public GridviewProcess() 12 { 13 // 14 //TODO: 在此处添加构造函数逻辑 15 // 16 } 17 #region 合并单元格 合并某一行的所有列 18 /// <summary> 19 /// 合并GridView中某行相同信息的行(单元格) 20 /// </summary> 21 /// <param name="GridView1">GridView对象</param> 22 /// <param name="cellNum">需要合并的行</param> 23 public static void GroupRow(GridView GridView1, int rows) 24 { 25 TableCell oldTc = GridView1.Rows[rows].Cells[0]; 26 for (int i = 1; i < GridView1.Rows[rows].Cells.Count; i++) 27 { 28 TableCell tc = GridView1.Rows[rows].Cells[i]; //Cells[0]就是你要合并的列 29 if (oldTc.Text == tc.Text) 30 { 31 tc.Visible = false; 32 if (oldTc.ColumnSpan == 0) 33 { 34 oldTc.ColumnSpan = 1; 35 } 36 oldTc.ColumnSpan++; 37 oldTc.VerticalAlign = VerticalAlign.Middle; 38 } 39 else 40 { 41 oldTc = tc; 42 } 43 } 44 } 45 #endregion 46 #region 合并单元格 合并一行中的几列 47 /// <summary> 48 /// 合并单元格 合并一行中的几列 49 /// </summary> 50 /// <param name="GridView1">GridView ID</param> 51 /// <param name="rows">行</param> 52 /// <param name="sCol">开始列</param> 53 /// <param name="eCol">结束列</param> 54 public static void GroupRow(GridView GridView1, int rows, int sCol, int eCol) 55 { 56 TableCell oldTc = GridView1.Rows[rows].Cells[sCol]; 57 for (int i = 1; i < eCol - sCol; i++) 58 { 59 TableCell tc = GridView1.Rows[rows].Cells[i + sCol]; //Cells[0]就是你要合并的列 60 tc.Visible = false; 61 if (oldTc.ColumnSpan == 0) 62 { 63 oldTc.ColumnSpan = 1; 64 } 65 oldTc.ColumnSpan++; 66 oldTc.VerticalAlign = VerticalAlign.Middle; 67 } 68 } 69 #endregion 70 #region 合并单元格 合并某一列所有行 71 /// <summary> 72 /// 合并GridView中某列相同信息的行(单元格) 73 /// </summary> 74 /// <param name="GridView1"></param> 75 /// <param name="cellNum"></param> 76 public static void GroupCol(GridView GridView1, int cols) 77 { 78 if (GridView1.Rows.Count < 1 || cols > GridView1.Rows[0].Cells.Count - 1) 79 { 80 return; 81 } 82 TableCell oldTc = GridView1.Rows[0].Cells[cols]; 83 for (int i = 1; i < GridView1.Rows.Count; i++) 84 { 85 TableCell tc = GridView1.Rows[i].Cells[cols]; 86 if (oldTc.Text == tc.Text) 87 { 88 tc.Visible = false; 89 if (oldTc.RowSpan == 0) 90 { 91 oldTc.RowSpan = 1; 92 } 93 oldTc.RowSpan++; 94 oldTc.VerticalAlign = VerticalAlign.Middle; 95 } 96 else 97 { 98 oldTc = tc; 99 } 100 } 101 } 102 #endregion 103 #region 合并单元格 合并某一列中的某些行 104 /// <summary> 105 /// 合并单元格 合并某一列中的某些行 106 /// </summary> 107 /// <param name="GridView1">GridView ID</param> 108 /// <param name="cellNum">列</param> 109 /// <param name="sRow">开始行</param> 110 /// <param name="eRow">结束列</param> 111 public static void GroupCol(GridView GridView1, int cols, int sRow, int eRow) 112 { 113 if (GridView1.Rows.Count < 1 || cols > GridView1.Columns.Count - 1) 114 { 115 return; 116 } 117 TableCell oldTc = GridView1.Rows[sRow].Cells[cols]; 118 for (int i = 1; i < eRow - sRow; i++) 119 { 120 TableCell tc = GridView1.Rows[sRow + i].Cells[cols]; 121 tc.Visible = false; 122 if (oldTc.RowSpan == 0) 123 { 124 oldTc.RowSpan = 1; 125 } 126 oldTc.RowSpan++; 127 oldTc.VerticalAlign = VerticalAlign.Middle; 128 } 129 } 130 #endregion 131 }
这是专门处理gridview的一个类,可以单独取出对自己有用的代码!
方法事列:
Griedview1.DataSource = dt;
Griedview1.DataBind();
GroupCol(Griedview1, 0);
注:在从列或者行开始的数字 是从0开始的!
作者:ruicky
出处:http://www.cnblogs.com/ruicky/
欢迎任何形式的转载,未经作者同意,请保留此段声明!