colspan gridview rows

前端 未结 2 940
萌比男神i
萌比男神i 2020-12-31 07:30

I have added rows into gridview. There are 20 columns in gridview. How can i do a colspan-like feature in gridview which could show me 2-3 rows under 2-3 columns and remaini

相关标签:
2条回答
  • 2020-12-31 07:54

    You need to handle the OnRowCreated event of the GridView as follows:

     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);
        }
    }
    

    Your markup should be something like this:

    <asp:GridView runat="server" ID="grid" OnRowCreated="grid_RowCreated" >
    

    On the above example, I populated the grid with this:

    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: sample image

    I just realized that you wanted to have the ROWS (not necessarily the header) to have certain colspan, in which case you can do:

     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);
        }
    }
    

    And it will produce:

    enter image description here

    0 讨论(0)
  • 2020-12-31 08:00

    BoundField and TemplateField tags has the property ItemStyle-Width="22%" as you can see you can set it for each column with a percentage to be responsive

    0 讨论(0)
提交回复
热议问题