Merge only one column at a time
# Region merge cells in the same column in The gridview
Public static void celltogether (INT cellindex, gridview gvtemp)
{
Int I = 0;
Int rowspannum = 1;
While (I <gvtemp. Rows. Count-1)
{
Gridviewrow gvr = gvtemp. Rows [I];
For (++ I; I <gvtemp. Rows. Count; I ++)
{
Gridviewrow gvrnext = gvtemp. Rows [I];
If (gvr. cells [cellindex]. Text = gvrnext. cells [cellindex]. Text)
{
// The contents of the First Batch column are consistent.
If (cellindex> = 1 )&&
(Gvr. cells [cellindex-1]. Text! = Gvrnext. cells [cellindex-1]. Text ))
{
Gvr. cells [cellindex]. rowspan = rowspannum;
Rowspannum = 1;
Break;
}
Else
{
Gvrnext. cells [cellindex]. Visible = false;
Rowspannum ++;
}
}
Else
{
Gvr. cells [cellindex]. rowspan = rowspannum;
Rowspannum = 1;
Break;
}
If (I = gvtemp. Rows. Count-1)
{
Gvr. cells [cellindex]. rowspan = rowspannum;
}
}
}
}
# Endregion
Usage
Called after the gridview is bound
Celltogetherarry (column number, gridview1 );
Multiple columns can be merged at a time (array)
Public static void celltogetherarry (INT [] cellindex, gridview gvtemp)
{
For (INT x = 0; x <cellindex. length; X ++)
{
Int I = 0;
Int rowspannum = 1;
While (I <gvtemp. Rows. Count-1)
{
Gridviewrow gvr = gvtemp. Rows [I];
For (++ I; I <gvtemp. Rows. Count; I ++)
{
Gridviewrow gvrnext = gvtemp. Rows [I];
If (gvr. cells [cellindex [x]. Text = gvrnext. cells [cellindex [X]. Text)
{
// The contents of the First Batch column are consistent.
If (cellindex [x]> = 1 )&&
(Gvr. cells [cellindex [x]-1]. Text! = Gvrnext. cells [cellindex [x]-1]. Text ))
{
Gvr. cells [cellindex [X]. rowspan = rowspannum;
Rowspannum = 1;
Break;
}
Else
{
Gvrnext. cells [cellindex [X]. Visible = false;
Rowspannum ++;
}
}
Else
{
Gvr. cells [cellindex [X]. rowspan = rowspannum;
Rowspannum = 1;
Break;
}
If (I = gvtemp. Rows. Count-1)
{
Gvr. cells [cellindex [X]. rowspan = rowspannum;
}
}
}
}
}
Usage
Int [] arr = new int [] {1, 2 };
Called after the gridview is bound
Celltogetherarry (ARR, gridview1 );