break;
}
if (i == GridVIEw1.Rows.Count - 1)
{
gvr.Cells[cellNum].RowSpan = rowSpanNum;
}
}
}
}
///
/// 根據條件列合並GridVIEw列中相同的行
///
/// GridVIEw對象
/// 需要合並的列
/// /// 條件列(根據某條件列還合並)
public static void GroupRows(GridView GridVIEw1, int cellNum, int cellNum2)
{
int i = 0, rowSpanNum = 1;
while (i < GridVIEw1.Rows.Count - 1)
{
GridViewRow gvr = GridVIEw1.Rows[i];
for (++i; i < GridVIEw1.Rows.Count; i++)
{
GridViewRow gvrNext = GridVIEw1.Rows[i];
if (gvr.Cells[cellNum].Text + gvr.Cells[cellNum2].Text == gvrNext.Cells[cellNum].Text + gvrNext.Cells[cellNum2].Text)
{
gvrNext.Cells[cellNum].Visible = false;
rowSpanNum++;
}
else
{
gvr.Cells[cellNum].RowSpan = rowSpanNum;
rowSpanNum = 1;
break;
if (i == GridVIEw1.Rows.Count - 1)
{
gvr.Cells[cellNum].RowSpan = rowSpanNum;
}
}
}
}
第二個方法只是在第一個方法的修改了一點點, 在判斷單元格的時候,加上了一個條件列:
if (gvr.Cells[cellNum].Text + gvr.Cells[cellNum2].Text == gvrNext.Cells[cellNum].Text + gvrNext.Cells[cellNum2].Text)。當然,第二個方法還可以擴展,根據多個列的條件來合並。