using system;
using system. collections. generic;
using system. LINQ;
using system. web;
using system. data;
using system. web. UI. webcontrols;
///
// gridviewmergecell merges the gridview Liyang 20090916
///
public class gridviewmergecell
{
Public gridviewmergecell ()
{< br> //
// todo: add the constructor logic here
//
}< br> # merge all columns in a row by Region Merging cells
Public static void grouprow (gridview)
{< br> for (INT rowindex = gridview. rows. count-2; rowindex> = 0; rowindex --)
{< br> 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 = previusrow. cells [I]. Text)
{
Row. cells [I]. rowspan = previusrow. cells [I]. rowspan <2? 2:
Previusrow. cells [I]. rowspan + 1;
Previusrow. cells [I]. Visible = false;
}
}
}
}
/// <Summary>
/// Merge rows with the same information in a row in the gridview (cell)
/// </Summary>
/// <Param name = "gridview1"> gridview object </param>
/// <Param name = "cellnum"> rows to be merged </param>
Public static void grouprow (gridview, int rows)
{
Tablecell oldtc = gridview. Rows [rows]. cells [0];
For (INT I = 1; I <gridview. Rows [rows]. cells. Count; I ++)
{
Tablecell Tc = gridview. Rows [rows]. cells [I]; // cells [0] is the column you want to merge
If (oldtc. Text = tc. text)
{
TC. Visible = false;
If (oldtc. columnspan = 0)
{
Oldtc. columnspan = 1;
}
Oldtc. columnspan ++;
Oldtc. verticalalign = verticalalign. Middle;
}
Else
{
Oldtc = tc;
}
}
}
# Endregion
# Region merge cells merge several columns in a row
/// <Summary>
/// Merge cells to merge several columns in a row
/// </Summary>
/// <Param name = "gridview1"> gridview id </param>
/// <Param name = "rows"> row </param>
/// <Param name = "Scol"> Start column </param>
/// <Param name = "Ecol"> end column </param>
Public static void grouprow (gridview, int rows, int Scol, int Ecol)
{
Tablecell oldtc = gridview. Rows [rows]. cells [Scol];
For (INT I = 1; I <Ecol-Scol; I ++)
{
Tablecell Tc = gridview. Rows [rows]. cells [I + Scol]; // cells [0] is the column you want to merge
TC. Visible = false;
If (oldtc. columnspan = 0)
{
Oldtc. columnspan = 1;
}
Oldtc. columnspan ++;
Oldtc. verticalalign = verticalalign. Middle;
}
}
# Endregion
# Region merge cells to merge all rows in a column
/// <Summary>
/// Merge rows with the same information in a column in the gridview (cell)
/// </Summary>
/// <Param name = "gridview1"> </param>
/// <Param name = "cellnum"> </param>
Public static void groupcol (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
{
Oldtc = tc;
}
}
}
# Endregion
# Region merge cells to merge certain rows in a column
/// <Summary>
/// Merge cells to merge certain rows in a column
/// </Summary>
/// <Param name = "gridview1"> gridview id </param>
/// <Param name = "cellnum"> column </param>
/// <Param name = "srow"> Start row </param>
/// <Param name = "erow"> end column </param>
Public static void groupcol (gridview, int cols, int srow, int erow)
{
If (gridview. Rows. Count <1 | Cols> gridview. Columns. Count-1)
{
Return;
}
Tablecell oldtc = gridview. Rows [srow]. cells [Cols];
For (INT I = 1; I <erow-srow; I ++)
{
Tablecell Tc = gridview. Rows [srow + I]. cells [Cols];
TC. Visible = false;
If (oldtc. rowspan = 0)
{
Oldtc. rowspan = 1;
}
Oldtc. rowspan ++;
Oldtc. verticalalign = verticalalign. Middle;
}
}
# Endregion
}