我已经在网格视图中添加了行。gridview中有20列。如何在gridview中实现类似于colspan的功能,它可以在2-3列下显示2-3行,并保留为colspan。
基本上,我希望在网格视图中对网格视图的行实现colspan。
因此,我现在的gv是这样的;
第1列2第3列4 ......第20列
Cell1 Cell2 Cell3单元4 ......单元格20 (用于第1行)
我希望有像这样的东西
第1列2第3列4 ......第20列
Cell1 Cell2 ...... Cell 20 (For Rows # 1)如果有任何疑问,请让我知道。
谢谢
发布于 2011-08-04 13:19:39
您需要按如下方式处理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();它产生这样的结果:

我刚刚意识到你想让行(不一定是标题)具有特定的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);
}
}它将产生:

发布于 2018-12-17 14:12:18
如您所见,BoundField和TemplateField标记的属性ItemStyle-Width="22%“,您可以为每一列设置响应百分比
https://stackoverflow.com/questions/6936417
复制相似问题