熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

Asp.net

2022-06-13   來源: .NET編程 
本文為大家介紹下如何實現動態生成表同一列中數據相同的單元格需要合並具體實現如下由此需求的朋友可以參考下希望對大家有所幫助  

  業務需要動態生成表同一列中數據相同的單元格需要合並
解決方案創建Table控件處理類代碼如下

復制代碼 代碼如下:
/// <summary>表格控件相關操作類
/// </summary>
public static class aspTable
{
/// <summary>合並行
/// </summary>
/// <remarks>版權信息::</remarks>
/// <param name="tbl">Table</param>
/// <param name="startRow">起始行</param>
/// <param name="endRow">結束行</param>
/// <param name="colIndex">要合並的列索引</param>
public static void SetRowSpan(Table tbl int startRow int endRow int colIndex)
{
int countRowSpan = ;
int spanRow = startRow;
string spanText = tblRows[startRow]Cells[colIndex]Text;
for (int rowIndex = startRow; rowIndex <= endRow; rowIndex++)
{
string currentText = tblRows[rowIndex]Cells[colIndex]Text;
//內容是否相同
if (currentText == spanText)
{
countRowSpan++;
//移除被合並的單元格
if (rowIndex != spanRow)
{
tblRows[rowIndex]CellsRemoveAt(colIndex);
}
}
else
{
//合並
tblRows[spanRow]Cells[colIndex]RowSpan = countRowSpan;
//從此行再向下比較(重置)
countRowSpan = ;
spanRow = rowIndex;
spanText = currentText;
}
}
//合並最後一項
tblRows[spanRow]Cells[colIndex]RowSpan = countRowSpan;
}
/// <summary>合並行支持多列
/// </summary>
/// <remarks><SPAN style="FONTFAMILY: Arial Helvetica sansserif">版權信息: Arial Helvetica sansserif"> ::</remarks></SPAN>
/// <param name="tbl">Table</param>
/// <param name="startRow">起始行</param>
/// <param name="endRow">結束行</param>
/// <param name="colIndex">要合並的列索引</param>
public static void SetRowSpans(Table tbl int startRow int endRow params int[] colIndexs)
{
ArrayList al = new ArrayList(colIndexs);
alSort();
for (int i = alCount ; i >= ; i)
{
SetRowSpan(tbl startRow endRow (int)al[i]);
}
}
}

  
需要注意的幾點起始行一般設置為因為是標題行結束行一般設置為Table的總行數即可(最後一行)


From:http://tw.wingwit.com/Article/program/net/201311/14044.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.