#region 合并单元格 合并某一行的所有列 public static void GroupRow(GridView gridView) { for (int rowIndex = gridView.Rows.Count - 2; rowIndex >= 0; rowIndex--) { GridViewRow row = gridView.Rows[rowIndex]; GridViewRow previousRow = gridView.Rows[rowIndex + 1]; for (int i = 0; i < row.Cells.Count; i++) { if (row.Cells[i].Text == previousRow.Cells[i].Text) { row.Cells[i].RowSpan = previousRow.Cells[i].RowSpan < 2 ? 2 : previousRow.Cells[i].RowSpan + 1; previousRow.Cells[i].Visible = false; } } } tc.Visible = to false; } /// <Summary> row /// combined GridView same information in a row (cells) /// </ Summary> /// <param name = "GridView1, and"> GridView Object </ param> /// <param name = "cellNum"> be merged row </ param> public static void GroupRow (the GridView GridView, int rows) { the TableCell oldTc = gridView.Rows [rows] .Cells [0]; for (int I =. 1; I <gridView.Rows [rows] .Cells.Count; I ++) { the TableCell gridView.Rows TC = [rows] .Cells [I]; // Cells [0] is the column to you if (oldTc.Text == tc.Text) { IF (oldTc.ColumnSpan == 0) { oldTc.ColumnSpan = 1; } oldTc.ColumnSpan ++; oldTc.VerticalAlign = VerticalAlign.Middle; } the else { oldTc = TC; } } } #endregion columns #region combined merged cell row /// <summary > /// combined merged cell columns in a row /// </ Summary> /// <param name = "GridView1, and"> the GridView ID </ param> /// <param name = "rows"> line < / param> /// <param name = "SCOL"> the starting column </ param> /// <param name = "ECOL"> end column </ param> static void GroupRow public (the GridView GridView, int rows, SCOL int, int ECOL) { the TableCell oldTc = gridView.Rows [rows] .Cells [SCOL]; for (int I =. 1; I <ECOL - SCOL; I ++) { the TableCell tc = gridView.Rows [rows] .Cells [ i + sCol]; // Cells [0] is the column to your tc.Visible = to false; IF (oldTc.ColumnSpan == 0) { oldTc.ColumnSpan =. 1; } oldTc.ColumnSpan ++; oldTc.VerticalAlign = VerticalAlign.Middle; } } #endregion #region a combined merged cell column for all rows /// <summary> /// GridView combined information in a column in the same row (cells) /// </ Summary > /// <param name = "GridView1, and"> </ param> /// <param name = "cellNum"> </ param> public static void GroupCol (the GridView GridView, int cols) { if (gridView.Rows.Count < 1 || cols > gridView.Rows[0].Cells.Count - 1) { return; } TableCell oldTc = gridView.Rows[0].Cells[cols]; for (int i = 1; i < gridView.Rows.Count; i++) { TableCell tc = gridView.Rows[i].Cells[cols]; if (oldTc.Text == tc.Text) { tc.Visible = false; if (oldTc.RowSpan == 0) { oldTc.RowSpan = 1; } OldTc.RowSpan ++; oldTc.VerticalAlign = VerticalAlign.Middle; } else { TC = oldTc; } } } #endregion certain row #region combined merged cell in a column /// <summary> certain row /// combined merged cell in a column /// </ Summary> / // <param name = "GridView1, and"> the GridView ID </ param> /// <param name = "cellNum"> column </ param> /// <param name = "sRow"> start OK </ param> / // <param name = "eRow" > end column </ param> public static void GroupCol (the GridView GridView, int cols, sRow int, int ERoW) { IF (gridView.Rows.Count <cols. 1 ||> gridView.Columns .count -. 1) { return; } OldTc = gridView.Rows the TableCell [sRow] .Cells [cols]; for (int I =. 1; I <ERoW - sRow; I ++) { the TableCell gridView.Rows TC = [I + sRow] .Cells [cols]; TC. = to false the Visible; IF (oldTc.RowSpan == 0) { oldTc.RowSpan =. 1; } oldTc.RowSpan ++; oldTc.VerticalAlign = VerticalAlign.Middle; } } #endregion }
above method is called as a time assignment GridView:
private void BindPurchaseOrders()
{
#region
DataTable ABC= s1.select("select * from Table ");
GridView.DataSource = ABC;
GridView.DataBind();
GroupCol (GridView, 0); // call the above method (this method for merging a first row of cells in the same data)
#endregion
// conditional merged cell
for (int i = 0; i <ABC.Rows.Count; i ++) // loop over all data
{
IF (ABC.Rows [I] [ "A"] the ToString () == the ABC. .Rows [i] [ "B" ]. ToString ()) // if the A field is equal to the merge field B cells
{
Suppose A // B in the first row in the second column, the combined method is called, a first row and second column of the same column merge data
GroupRow (GV_LowValue, I, 0,. 1);
}
}
}