Business needs, dynamically generating tables, and cells with the same data in the same column need to be merged.
Solution, create a Table control handler class with the following code:
Copy Code code as follows:
<summary> Table control related action class
</summary>
public static Class Asptable
{
<summary> Merge Line
</summary>
<remarks> Copyright: Http://www.qqextra.com,http://t.qq.com/ls_man,http://blog.csdn.net/ls_man 2013-06-21 14:20:36</remarks>
<param name= "TBL" >Table</param>
<param name= "StartRow" > Start line </param>
<param name= "Endrow" > End line </param>
<param name= "Colindex" > column index to merge </param>
public static void Setrowspan (Table tbl, int startrow, int endrow, int colindex)
{
int countrowspan = 0;
int spanrow = StartRow;
String spantext = tbl. Rows[startrow]. Cells[colindex]. Text;
for (int rowIndex = StartRow; RowIndex <= endrow; rowindex++)
{
String currenttext = tbl. Rows[rowindex]. Cells[colindex]. Text;
Content is the same
if (Currenttext = = Spantext)
{
countrowspan++;
Remove merged cells
if (RowIndex!= spanrow)
{
Tbl. Rows[rowindex]. Cells.removeat (Colindex);
}
}
Else
{
Merge
Tbl. Rows[spanrow]. Cells[colindex]. RowSpan = Countrowspan;
Compare down (reset) from this line
Countrowspan = 0;
Spanrow = rowindex--;
Spantext = Currenttext;
}
}
Merging the last item
Tbl. Rows[spanrow]. Cells[colindex]. RowSpan = Countrowspan;
}
<summary> merging rows, supporting multiple columns
</summary>
<remarks><span style= "font-family:arial, Helvetica, sans-serif" > Copyright information: http://www.qqextra.com,http:/ /t.qq.com/ls_man,http://blog.csdn.net/ls_man</span><span style= "font-family:arial, Helvetica, Sans-serif "> 2013-06-21 15:24:34</remarks></span>
<param name= "TBL" >Table</param>
<param name= "StartRow" > Start line </param>
<param name= "Endrow" > End line </param>
<param name= "Colindex" > column index to merge </param>
public static void Setrowspans (Table tbl, int startrow, int endrow, params int[] colindexs)
{
ArrayList al = new ArrayList (COLINDEXS);
Al. Sort ();
for (int i = al. Count-1; I >= 0; i--)
{
Setrowspan (TBL, StartRow, Endrow, (int) al[i]);
}
}
}
Note that the starting line is generally set to 1 because 0 is the header row, and the end line is generally set to the total row number of the table-1 (the last line).