Merge rows (cells) with the same information in a column (including the template column) in the GridView)

Source: Internet
Author: User

When creating a project, you need to merge columns with the same information in the gridview. So google found the following example:

Solution ):

1 /// <summary>
2 // merge rows with the same information in a column in the GridView (cell)
3 /// </summary>
4 /// <param name = "GridView1"> GridView </param>
5 /// <param name = "cellNum"> column number </param>
6 public static void GroupRows (GridView GridView1, int cellNum)
7 {
8 int I = 0, rowSpanNum = 1;
9 while (I <GridView1.Rows. Count-1)
10 {
11 GridViewRow gvr = GridView1.Rows [I];
12
13 for (++ I; I <GridView1.Rows. Count; I ++)
14 {
15 GridViewRow gvrNext = GridView1.Rows [I];
16 if (gvr. Cells [cellNum]. Text = gvrNext. Cells [cellNum]. Text)
17 {
18 gvrNext. Cells [cellNum]. Visible = false;
19 rowSpanNum ++;
20}
21 else
22 {
23 gvr. Cells [cellNum]. RowSpan = rowSpanNum;
24 rowSpanNum = 1;
25 break;
26}
27
28 if (I = GridView1.Rows. Count-1)
29 {
30 gvr. Cells [cellNum]. RowSpan = rowSpanNum;
31}
32}
33}
34}
35
36

But when I copy the code to my project, I cannot get the desired result, which is equivalentJinma storeSo I read the code again and found that there was no problem. After being depressed for a long time, I found that cells [cellNum] in the above example. the text value is the value in the row, and the merged value isTemplate ColumnLable in, modified:

 

Int I = 0, rowSpanNum = 1;
Int cellNum = 0 ;//Here, the cellnum value is the index of the column you want to merge, starting from 0.
While (I <GridView1.Rows. Count-1)
{
GridViewRow gvr = GridView1.Rows [I];

Label tmplable = (Label) gvr. FindControl ("UpUnitName ");

For (++ I; I <GridView1.Rows. Count; I ++)
{
GridViewRow gvrNext = GridView1.Rows [I];
Label tmplable1 = (Label) gvrNext. FindControl ("UpUnitName ");


If (tmplable. Text = tmplable1.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;
}
}

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.