Code for merging the same columns of the GridView and DataGrid in asp.net

Source: Internet
Author: User

(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;
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.