http://www.cnblogs.com/linbaoji/archive/2009/08/24/1553166.html感谢牛人 // <summary> /// 合并GridView列中相同的行 :证实可用 /// </summary> /// <param name="gridView">GridView对象</param> /// <param name="cellNum">需要合并的列</param> public void GroupRows(GridView gridView, int cellNum) { int i = 0, rowSpanNum = 1; while (i < gridView.Rows.Count - 1) { GridViewRow gvr = gridView.Rows[i]; for (++i; i < gridView.Rows.Count; i++) { GridViewRow gvrNext = gridView.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 == gridView.Rows.Count - 1) { gvr.Cells[cellNum].RowSpan = rowSpanNum; } } } }
private void BindData()//GridView 数据绑定 { this.gridViewExample.DataSource = getDataTable(); this.gridViewExample.DataBind(); GroupRows(gridViewExample, 0);//合并第一列 GroupRows(gridViewExample, 1); //合并第二列 }
private DataTable getDataTable()//数据绑定源 { DataTable dt = new DataTable("gvExample");
DataColumn dc = new DataColumn(); dc.ColumnName = "userid"; dt.Columns.Add(dc); dc = new DataColumn(); dc.ColumnName = "username"; dt.Columns.Add(dc); dc = new DataColumn(); dc.ColumnName = "userlove"; dt.Columns.Add(dc);
DataRow dr = dt.NewRow(); dr["userid"] = "1"; dr["username"] = "vincent"; dr["userlove"] = "swim"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["userid"] = "1"; dr["username"] = "vincent"; dr["userlove"] = "read"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["userid"] = "1"; dr["username"] = "vincent"; dr["userlove"] = "history"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["userid"] = "2"; dr["username"] = "green"; dr["userlove"] = "coding"; dt.Rows.Add(dr); return dt; }
|