業(yè)務(wù)需要,動態(tài)生成表,同一列中數(shù)據(jù)相同的單元格需要合并。
解決方案,創(chuàng)建Table控件處理類,代碼如下:
復制代碼 代碼如下:
/// summary>表格控件相關(guān)操作類
/// /summary>
public static class aspTable
{
/// summary>合并行
/// /summary>
/// remarks>版權(quán)信息: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">起始行/param>
/// param name="endRow">結(jié)束行/param>
/// param name="colIndex">要合并的列索引/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;
//內(nèi)容是否相同
if (currentText == spanText)
{
countRowSpan++;
//移除被合并的單元格
if (rowIndex != spanRow)
{
tbl.Rows[rowIndex].Cells.RemoveAt(colIndex);
}
}
else
{
//合并
tbl.Rows[spanRow].Cells[colIndex].RowSpan = countRowSpan;
//從此行再向下比較(重置)
countRowSpan = 0;
spanRow = rowIndex--;
spanText = currentText;
}
}
//合并最后一項
tbl.Rows[spanRow].Cells[colIndex].RowSpan = countRowSpan;
}
/// summary>合并行,支持多列
/// /summary>
/// remarks>SPAN style="FONT-FAMILY: Arial, Helvetica, sans-serif">版權(quán)信息: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">起始行/param>
/// param name="endRow">結(jié)束行/param>
/// param name="colIndex">要合并的列索引/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]);
}
}
}
需要注意的幾點,起始行一般設(shè)置為1,因為0是標題行;結(jié)束行一般設(shè)置為Table的總行數(shù)-1即可(最后一行)。
您可能感興趣的文章:- datalist,Repeater和Gridview的區(qū)別分析
- asp.net中讓Repeater和GridView支持DataPager分頁
- repeater、gridview 在綁定時判斷判斷顯示不同的行樣式或文本
- ASP.NET MVC4之js css文件合并功能(3)
- Asp.net程序優(yōu)化js、css實現(xiàn)合并與壓縮的方法
- ASP.NET GridView 實現(xiàn)課程表顯示(動態(tài)合并單元格)實現(xiàn)步驟
- asp.net中GridView和DataGrid相同列合并實現(xiàn)代碼
- asp.net中rdlc 合并行的方法
- asp.net 合并GridView中某列相同信息的行(單元格)
- ASP.NET中GridView和Repeater重復數(shù)據(jù)如何合并