colspan gridview行
我在gridview中添加了行。 gridview中有20列。 我怎么能在gridview中做一个类似colspan的function,它可以在2-3列下显示2-3行并保持为colspan。
基本上我希望在gridview的行上实现gridview中的colspan。
因此我现在的gv就像;
Col 1 Col 2 Col 3 Col 4 …… Col 20
Cell1 Cell2 Cell3 Cell 4 …… Cell 20(行#1)
我希望有类似的东西
Col 1 Col 2 Col 3 Col 4 …… Col 20
Cell1 Cell2 ...... Cell 20 (For Rows # 1)
如有任何疑问,请与我们联系。
谢谢
您需要处理GridView的OnRowCreated事件,如下所示:
protected void grid_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.Header) { e.Row.Cells[2].ColumnSpan = 2; //now make up for the colspan from cell2 e.Row.Cells.RemoveAt(4); } }
你的标记应该是这样的:
在上面的例子中,我用这个填充了网格:
DataTable dt = new DataTable(); for (int i = 0; i < 5; i++) { dt.Columns.Add("Col " + i); } for (int i = 0; i < 10; i++) { DataRow r = dt.NewRow(); r.ItemArray=new object[]{"row "+i,"row "+i,"row "+i,"row "+i,"row "+i}; dt.Rows.Add(r); } grid.DataSource = dt; grid.DataBind();
它产生了这个:
我刚刚意识到你想让ROWS(不一定是标题)有一定的colspan,在这种情况下你可以这样做:
protected void grid_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Cells[2].ColumnSpan = 2; //now make up for the colspan from cell2 e.Row.Cells.RemoveAt(4); } }
它会产生: