Asp.net_table Control Cell Vertical Merge Example _ practical tips

Source: Internet
Author: User

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).

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.