colspan gridview行

xor*_*wer 14 .net c# asp.net gridview visual-studio

我在gridview中添加了行.gridview中有20列.我如何在gridview中执行类似colspan的功能,它可以在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)
Run Code Online (Sandbox Code Playgroud)

如有任何疑问,请告诉我.

谢谢

Ica*_*rus 26

您需要处理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);
    }
}
Run Code Online (Sandbox Code Playgroud)

你的标记应该是这样的:

<asp:GridView runat="server" ID="grid" OnRowCreated="grid_RowCreated" >
Run Code Online (Sandbox Code Playgroud)

在上面的例子中,我用这个填充了网格:

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();
Run Code Online (Sandbox Code Playgroud)

它产生了这个: 样本图像

我刚刚意识到你想让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);
    }
}
Run Code Online (Sandbox Code Playgroud)

它会产生:

在此输入图像描述