///
/// 合并GridView列中相同的行
///
/// GridView對象
/// 需要合并的列
public static void GroupRows(GridView GridView1, int cellNum)
{
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 == gvrNext.Cells[cellNum].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;
}
}
}
}
///
/// 根據條件列合并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)。當然,第二個方法還可以擴展,根據多個列的條件來合并。
posted on 2010-10-14 12:02
無聲 閱讀(242)
評論(0) 編輯 收藏