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.