Merge DataGrid rows in Asp.net

Source: Internet
Author: User

For example:

Serial number name measuring unit
1. RMB in Industrial Economy
I. Industrial Economy bbb

Merged:

Serial number name measuring unit
1. RMB in Industrial Economy
Bbb
How to merge multiple rows in the first column into one row? Thank you!

 

Implementation Method:
Let's change the stupid brother to the following:
On the. ASPX page, use <asp: DataGrid>
Onprerender = "fdgrid_prerender">

In the. CS file:

// Merge the same cells.
Public void fdgrid_prerender (Object sender, system. eventargs E)
{
If (this. fdgrid. Items. Count <= 1)
{
Return;
}
Col = 0;
Tablecell oldtc = This. fdgrid. items [0]. cells [col];
For (INT I = 1; I <this. fdgrid. Items. Count; I ++)
{
Tablecell Tc = This. fdgrid. items [I]. cells [col];
If (TC. Text = oldtc. Text)
{
TC. Visible = false;
If (oldtc. rowspan = 0)
{
Oldtc. rowspan = 1;
}
Oldtc. rowspan = oldtc. rowspan + 1;
Oldtc. verticalalign = verticalalign. Middle;
}
Else
{
Oldtc = tc;
}
}
}

You can also use the itemdatabound event. The details are as follows:

In the. CS file
Add the following to the initializecomponent method:

This. dgcontacts. itemdatabound + = new system. Web. UI. webcontrols. datagriditemeventhandler (this. dgcontacts_itemdatabound );

Add the following to page_load in the. CS file:
If (! Page. ispostback)
{
Lastindex = 0;
}

Dgcontacts is the DataGrid name.

Add the following to the. CS file: Code :
Int lastindex;
Protected void dgcontacts_itemdatabound (Object source,
System. Web. UI. webcontrols. datagriditemeventargs E)
{
If (E. Item. itemtype = listitemtype. Item | E. Item. itemtype = listitemtype. alternatingitem)
{
String ismanager = (string) databinder. eval (E. Item. dataitem, "No ");
Int inn = E. Item. itemindex;
Tablecell C;
Int Col = 0;
If (INN> 0)
{
If (dgcontacts. items [lastindex]. cells [col]. Text = ismanager)
{

C = new tablecell ();
C = E. Item. cells [col];
If (dgcontacts. items [lastindex]. cells [col]. rowspan = 0)
Dgcontacts. items [lastindex]. cells [col]. rowspan = 1;

Dgcontacts. items [lastindex]. cells [col]. rowspan + = 1;

Response. Write (dgcontacts. items [lastindex]. cells [col]. rowspan );
E. Item. cells. Remove (C );

}
Else
{
E. Item. cells [col]. Text = ismanager;
Lastindex = E. Item. itemindex;
}
}
Else
{
E. Item. cells [col]. Text = ismanager;
}

}
}

Both methods are acceptable, but the first method is good and versatile. The second method should be fine if it is slightly modified. There may be other methods. The specific method is not important. What is important is how to flexibly apply basic knowledge to solve complicated problems.

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.