Asp.net _ Table Control Merge cells vertically

Source: Internet
Author: User

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:


[Plain]
/// <Summary> table control related operation class
/// </Summary>
Public static class aspTable
{
/// <Summary> merge rows
/// </Summary>
/// <Remarks> copyright: 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: 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]);
}
}
}

/// <Summary> table control related operation class
/// </Summary>
Public static class aspTable
{
/// <Summary> merge rows
/// </Summary>
/// <Remarks> copyright: 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> copyright information 15:24:34 </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 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: 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 ).

 


 

 


 

Related Article

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.