解決方法如下(寫在公共模塊裡,定義為靜態方法方便調用):
復制代碼 代碼如下:
/// <summary>
/// 合並GridView中某列相同信息的行(單元格)
/// </summary>
/// <param name="GridView1">GridView</param>
/// <param name="cellNum">第幾列</param>
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;
}
}
}
}
如果多列要求合並時,可作適當修改。
一個完整的人生應該是寬恕、容忍、等待和愛!