我已将行添加到 gridview 中。 gridview中有20列。我如何在 gridview 中执行类似 colspan 的功能,该功能可以在 2-3 列下显示 2-3 行,并保留为 colspan。
基本上我希望在 gridview 的行上实现 colspan 。
因此我现在的 gv 是这样的;
第 1 栏 2 栏 3 栏 4 ...... 第 20 栏
Cell1 Cell2 Cell3 Cell 4 ...... Cell 20(对于第 1 行)
我希望有类似的东西
第 1 栏 2 栏 3 栏 4 ...... 第 20 栏
Cell1 Cell2 ...... Cell 20 (For Rows # 1)
如有任何疑问,请告诉我。
Thanks
您需要按如下方式处理 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);
}
}
你的标记应该是这样的:
<asp:GridView runat="server" ID="grid" OnRowCreated="grid_RowCreated" >
在上面的示例中,我用以下内容填充了网格:
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();
And it produces this:
我刚刚意识到您希望 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);
}
}
它会产生:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)