一样的Cell合併成一个
本来
目标
开始
public bool isSearch = false;
在bind之前将isSearch=true;
拉一个GridView ID为GridView1,在事件PreRender,加入以下code
protected void GridView1_PreRender(object sender, EventArgs e) { if (isSearch == false) return; isSearch = false; for (int i = gvR.DataItemIndex + 1; i < this.GridView1.Rows.Count; i++) { //与下一列栏位值相等; if (gvR.Cells[0].Text.Trim() == this.GridView1.Rows[i].Cells[0].Text.Trim()) { gvR.Cells[0].RowSpan = gvR.Cells[0].RowSpan == 0 ? gvR.Cells[0].RowSpan = 2 : gvR.Cells[0].RowSpan += 1; this.GridView1.Rows[i].Cells[0].Visible = false; } else { break; } } } }
如果col是样板,就要用findeControl取值
```
protected void GridView1_PreRender(object sender, EventArgs e)
{
if (isSearch == false) return;
isSearch = false;
foreach (GridViewRow gvR in this.GridView1.Rows) { for (int i = gvR.DataItemIndex + 1; i < this.GridView1.Rows.Count; i++) { string x1 = ((Label)gvR.FindControl("col1")).Text; string x2 = ((Label)this.GridView1.Rows[i].FindControl("col1")).Text; //与下一列栏位值相等; if (x1== x2) { gvR.Cells[0].RowSpan = gvR.Cells[0].RowSpan == 0 ? gvR.Cells[0].RowSpan = 2 : gvR.Cells[0].RowSpan += 1; this.GridView1.Rows[i].Cells[0].Visible = false; } else { break; } } } }