(1) Common Columns
Copy codeThe Code is as follows:
/// <Summary>
/// Merge the Gridview columns (Common columns, excluding template columns)
/// Note: 1. The same rows can be put together by grouping and sorting when the GridView is bound.
/// 2. The method application time should be used in the DataBound event of the Gridview
/// </Summary>
/// <Param name = "gv"> the GridView object to be merged </param>
/// <Param name = "columnIndex"> index of the column to be merged </param>
Public static void UnitCell (GridView gv, int columnIndex)
{
Int I = 0; // the current number of rows.
String lastType = string. Empty; // determines whether to merge the values of the columns corresponding to the row.
Int lastCell = 0; // determines the index of the last row with the same value
If (gv. Rows. Count> 0)
{
LastType = gv. Rows [0]. Cells [columnIndex]. Text. ToString ();
Gv. Rows [0]. Cells [columnIndex]. RowSpan = 1;
LastCell = 0;
}
For (I = 1; I <gv. Rows. Count; I ++)
{
If (gv. Rows [I]. Cells [columnIndex]. Text = lastType)
{
Gv. Rows [I]. Cells [columnIndex]. Visible = false;
Gv. Rows [lastCell]. Cells [columnIndex]. RowSpan ++;
}
Else
{
LastType = gv. Rows [I]. Cells [columnIndex]. Text. ToString ();
LastCell = I;
Gv. Rows [I]. Cells [columnIndex]. RowSpan = 1;
}
}
}
/// <Summary>
/// Merge DataGrid columns (Common columns, excluding template columns)
/// Note: 1. When the DataGrid is bound, it can be grouped and sorted to put the same rows together.
/// 2. The method application time should be used in the DataBound event of the DataGrid.
/// </Summary>
/// <Param name = "dg"> the DataGrid object to be merged </param>
/// <Param name = "columnIndex"> index of the column to be merged </param>
Public static void UnitCell_T (DataGrid dg, int columnIndex)
{
Int I = 0; // the current number of rows.
String lastType = string. Empty; // determines whether to merge the values of the columns corresponding to the row.
Int lastCell = 0; // determines the index of the last row with the same value
If (dg. Items. Count> 0)
{
LastType = dg. Items [0]. Cells [columnIndex]. Text. ToString ();
Dg. Items [0]. Cells [columnIndex]. RowSpan = 1;
LastCell = 0;
}
For (I = 1; I <dg. Items. Count; I ++)
{
If (dg. Items [I]. Cells [columnIndex]. Text = lastType)
{
Dg. Items [I]. Cells [columnIndex]. Visible = false;
Dg. Items [lastCell]. Cells [columnIndex]. RowSpan ++;
}
Else
{
LastType = dg. Items [I]. Cells [columnIndex]. Text. ToString ();
LastCell = I;
Dg. Items [I]. Cells [columnIndex]. RowSpan = 1;
}
}
}
(2) template Columns
Copy codeThe Code is as follows:
/// <Summary>
/// Merge the Gridview columns (template column)
/// </Summary>
/// <Param name = "gv"> the GridView object to be merged </param>
/// <Param name = "columnIndex"> index of the column to be merged </param>
/// <Param name = "lblName"> ID of the template column object </param>
Public static void UnitCell (GridView gv, int columnIndex, string lblName)
{
Int I = 0; // the current number of rows.
String lastType = string. Empty; // determines whether to merge the values of the columns corresponding to the row.
Int lastCell = 0; // determines the index of the last row with the same value
If (gv. Rows. Count> 0)
{
LastType = (gv. Rows [0]. Cells [columnIndex]. FindControl (lblName) as Label). Text;
Gv. Rows [0]. Cells [columnIndex]. RowSpan = 1;
LastCell = 0;
}
For (I = 1; I <gv. Rows. Count; I ++)
{
If (gv. Rows [I]. Cells [columnIndex]. FindControl (lblName) as Label). Text = lastType)
{
Gv. Rows [I]. Cells [columnIndex]. Visible = false;
Gv. Rows [lastCell]. Cells [columnIndex]. RowSpan ++;
}
Else
{
LastType = (gv. Rows [I]. Cells [columnIndex]. FindControl (lblName) as Label). Text. ToString ();
LastCell = I;
Gv. Rows [I]. Cells [columnIndex]. RowSpan = 1;
}
}
}
(3) Call It In The DataBound event.
Display columns of fixed width in the GridView or DataGrid
By default, the Gridview and Datagrid controls automatically adjust the column size based on their content. To specify a fixed Width for a column, set the Width attribute of each Tablecell object and set the Wrap attribute to False. The following example shows how to use the DataGrid Control.
Copy codeThe Code is as follows:
Rotected void maid (object sender, maid e)
{
ListItemType detail = e. Item. ItemType;
If (response = ListItemType. Header)
{
For (int I = 0; I <e. Item. Cells. Count; I ++)
{
E. Item. Cells [I]. Width = Unit. Pixel (50 );
E. Item. Cells [I]. Wrap = false;
}
}
}