2012年9月17日 星期一
{C#} Gridview 欄位合並
//結果圖:
//針對前三欄如上一欄資料與下一欄資料相同,則合並
protected void MainDG_PreRender(object sender, EventArgs e)
{
//分格Row
int i = 1;
foreach (GridViewRow item in MainDG.Rows)
{
if (item.RowIndex != 0)
{
if (item.Cells[0].Text.Trim() == MainDG.Rows[item.RowIndex - i].Cells[0].Text.Trim() && item.Cells[1].Text.Trim() == MainDG.Rows[item.RowIndex - i].Cells[1].Text.Trim()
&& item.Cells[2].Text.Trim() == MainDG.Rows[item.RowIndex - i].Cells[2].Text.Trim() && item.Cells[3].Text.Trim() == MainDG.Rows[item.RowIndex - i].Cells[3].Text.Trim())
{
MainDG.Rows[(item.RowIndex - i)].Cells[0].RowSpan += 1;
item.Cells[0].Visible = false;
MainDG.Rows[(item.RowIndex - i)].Cells[1].RowSpan += 1;
item.Cells[1].Visible = false;
MainDG.Rows[(item.RowIndex - i)].Cells[2].RowSpan += 1;
item.Cells[2].Visible = false;
MainDG.Rows[(item.RowIndex - i)].Cells[3].RowSpan += 1;
item.Cells[3].Visible = false;
i++;
}
else
{
MainDG.Rows[(item.RowIndex)].Cells[0].RowSpan += 1;
MainDG.Rows[(item.RowIndex)].Cells[1].RowSpan += 1;
MainDG.Rows[(item.RowIndex)].Cells[2].RowSpan += 1;
MainDG.Rows[(item.RowIndex)].Cells[3].RowSpan += 1;
i = 1;
}
}
else
{
item.Cells[0].RowSpan = 1;
item.Cells[1].RowSpan = 1;
item.Cells[2].RowSpan = 1;
item.Cells[3].RowSpan = 1;
}
}
}
參考資料:
http://www.dotblogs.com.tw/mis2000lab/archive/2008/04/24/3451.aspx
http://epaper.blueshop.com.tw/board/show.asp?subcde=BRD20061225110536B4G&fumcde=FUM20050124192253INM
http://pramaire.pixnet.net/blog/post/28100706-gridview%E6%A8%99%E9%A1%8C%28%E8%A1%A8%E9%A0%AD%29%E5%90%88%E4%BD%B5