Business Needs: dynamically generate tables. cells with the same data in the same column must be merged.
Solution: Create a Table control processing class. The Code is as follows:
Copy codeThe Code is as follows:
/// <Summary> table control related operation class
/// </Summary>
Public static class aspTable
{
/// <Summary> merge rows
/// </Summary>
/// <Remarks> copyright: http://www.qqextra.com, http://t.qq.com/ls_man,http://blog.csdn.net/ls_man 14:20:36 </remarks>
/// <Param name = "tbl"> Table </param>
/// <Param name = "startRow"> Start row </param>
/// <Param name = "endRow"> end row </param>
/// <Param name = "colIndex"> column index to be merged </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;
// Whether the content is the same
If (currentText = spanText)
{
CountRowSpan ++;
// Remove the merged Cell
If (rowIndex! = SpanRow)
{
Tbl. Rows [rowIndex]. Cells. RemoveAt (colIndex );
}
}
Else
{
// Merge
Tbl. Rows [spanRow]. Cells [colIndex]. RowSpan = countRowSpan;
// Compare from this row (reset)
CountRowSpan = 0;
SpanRow = rowIndex --;
SpanText = currentText;
}
}
// Merge the last item
Tbl. Rows [spanRow]. Cells [colIndex]. RowSpan = countRowSpan;
}
/// <Summary> merge rows. Multiple columns are supported.
/// </Summary>
/// <Remarks> <SPAN style = "FONT-FAMILY: Arial, Helvetica, sans-serif"> copyright: 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"> 15:24:34 </remarks> </SPAN>
/// <Param name = "tbl"> Table </param>
/// <Param name = "startRow"> Start row </param>
/// <Param name = "endRow"> end row </param>
/// <Param name = "colIndex"> column index to be merged </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 row is generally set to 1, because 0 is the title row, and the ending row is generally set to the total number of rows in the Table-1 (the last row ).