PreRender是在頁面呈現之前去觸發的所以選擇寫在GridView1_PreRender。
protected void GridView1_PreRender1(object sender, EventArgs e) { int i = 1; foreach (GridViewRow gvItem in GridView1.Rows) { if (gvItem.RowIndex != 0) { //比對如果名稱如果相同就合併(RowSpan+1) if (gvItem.Cells[0].Text.Trim() == GridView1.Rows[(gvItem.RowIndex - i)].Cells[0].Text.Trim()) { GridView1.Rows[(gvItem.RowIndex - i)].Cells[0].RowSpan += 1; gvItem.Cells[0].Visible = false; i = i + 1; } else { GridView1.Rows[(gvItem.RowIndex)].Cells[0].RowSpan += 1; i = 1; } } else { gvItem.Cells[0].RowSpan = 1; } } }
結果:
全站熱搜
留言列表