http://www.codeproject.com/KB/webforms/MergeGridViewCells.aspx
public class GridDecorator
{
public static void MergeRows(GridView gridView)
{
for (int rowIndex = gridView.Rows.Count - 2; rowIndex >= 0; rowIndex--)
{
GridViewRow row = gridView.Rows[rowIndex];
GridViewRow previousRow = gridView.Rows[rowIndex + 1];
for (int i = 0; i < row.Cells.Count; i++)
{
if (row.Cells[i].Text == previousRow.Cells[i].Text)
{
row.Cells[i].RowSpan = previousRow.Cells[i].RowSpan < 2 ? 2 :
previousRow.Cells[i].RowSpan + 1;
previousRow.Cells[i].Visible = false;
}
}
}
}
}
protected void gridView_PreRender(object sender, EventArgs e)
{
GridDecorator.MergeRows(gridView);
}
public static void JoinRows(GridView gvList) { ///合并GridView重复的职务分类 int rowspan = 1; int count = gvList.Rows.Count; //为总计的时候GridViewRow 设置不能反查 GridViewRow currentgvr; for (int j = gvList.Rows.Count - 1; j >= 0; j--) { currentgvr = gvList.Rows[j]; if (currentgvr.Cells[0].Text == "总计") { foreach (Control cols in currentgvr.Controls) { foreach (Control col in cols.Controls) { if (col.GetType().Name == typeof(LinkButton).Name) { col.Visible = false; } if (col.GetType().Name == typeof(Label).Name) { col.Visible = true; } } } } } for (int i = 0; i < count; i++) { currentgvr = gvList.Rows[i]; currentgvr.Cells[0].Visible = false; if (i == count - 1) { gvList.Rows[i + 1 - rowspan].Cells[0].Visible = true; gvList.Rows[i + 1 - rowspan].Cells[0].RowSpan = rowspan; break; } GridViewRow gvrNext = gvList.Rows[i + 1]; if (currentgvr.Cells[0].Text == gvrNext.Cells[0].Text) { rowspan++; } else { gvList.Rows[i + 1 - rowspan].Cells[0].Visible = true; gvList.Rows[i + 1 - rowspan].Cells[0].RowSpan = rowspan; rowspan = 1; } } } Class2 cl2 = new Class2(); string temp = ""; for (int i = 0; i < GridView1.Rows.Count; i++) { if (((TextBox)GridView1.Rows[i].FindControl("textbox1")).Text.ToString().Trim() == "") { Response.Write(""); return; } } for (int ii = 0; ii <= GridView1.Rows.Count - 1; ii++) { //cl2.updatefolder(string user, string name, string nameid) cl2.updatefolder(id, ((TextBox)GridView1.Rows[ii].FindControl("textbox1")).Text.ToString().Trim(),GridView1.DataKeys[GridView1.Rows[ii].RowIndex].Value.ToString()); // } } } this.GridView1.DataSource = cl2.favoriteFolder(id); this.GridView1.DataKeyNames = new string[] { "folderid" }; this.GridView1.Columns[0].Visible = false; this.GridView1.DataBind();