Merge cells in the gridview

Source: Internet
Author: User
// <Summary>
/// Merge the same rows in the gridview Column
/// </Summary>
/// <Param name = "gridview1"> gridview object </param>
/// <Param name = "cellnum"> columns to be merged </param>
Public static void grouprows (gridview gridview1, int cellnum)
{
Int I = 0, rowspannum = 1;
While (I <gridview1.rows. Count-1)
{
Gridviewrow gvr = gridview1.rows [I];
For (++ I; I <gridview1.rows. Count; I ++)
{
Gridviewrow gvrnext = gridview1.rows [I];
If (gvr. cells [cellnum]. Text = gvrnext. cells [cellnum]. Text)
{
Gvrnext. cells [cellnum]. Visible = false;
Rowspannum ++;
}
Else
{
Gvr. cells [cellnum]. rowspan = rowspannum;
Rowspannum = 1;
Break;
}

If (I = gridview1.rows. Count-1)
{
Gvr. cells [cellnum]. rowspan = rowspannum;
}
}
}
}

/// <Summary>
/// Merge the same rows in the gridview Column Based on the condition column
/// </Summary>
/// <Param name = "gridview1"> gridview object </param>
/// <Param name = "cellnum"> columns to be merged </param>
///// <Param name = "cellnum2"> condition column (which is also merged based on a condition column) </param>
Public static void grouprows (gridview gridview1, int cellnum, int cellnum2)
{
Int I = 0, rowspannum = 1;
While (I <gridview1.rows. Count-1)
{
Gridviewrow gvr = gridview1.rows [I];
For (++ I; I <gridview1.rows. Count; I ++)
{
Gridviewrow gvrnext = gridview1.rows [I];
If (gvr. cells [cellnum]. Text + gvr. cells [cellnum2]. Text = gvrnext. cells [cellnum]. Text + gvrnext. cells [cellnum2]. Text)
{
Gvrnext. cells [cellnum]. Visible = false;
Rowspannum ++;
}
Else
{
Gvr. cells [cellnum]. rowspan = rowspannum;
Rowspannum = 1;
Break;
}

If (I = gridview1.rows. Count-1)
{
Gvr. cells [cellnum]. rowspan = rowspannum;
}
}
}
}

 

The second method is only slightly modified in the first method. When determining the cell, a condition column is added:

If (gvr. cells [cellnum]. Text + gvr. cells [cellnum2]. Text = gvrnext. cells [cellnum]. Text + gvrnext. cells [cellnum2]. text ). Of course, the second method can be extended and merged based on the conditions of multiple columns.

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.