Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET WebForms - How to set colspan for header row in the GridView?

I want to set colspan for the header row in the grid view to look the same as on the image below:

alt text

Html code is:

<html>
    <body>
        <table border="1">
        <tr>
            <th colspan=2>Header</th>
        </tr>
        <tr>
            <td>row 1, cell 1</td>
            <td>row 1, cell 2</td>
        </tr>
        <tr>
            <td>row 2, cell 1</td>
            <td>row 2, cell 2</td>
        </tr>
        </table>
    </body>
</html>

I don't know how to create the same effect in the asp.net and I don't want to create the table by hand using for loops.

Thank you!

like image 410
šljaker Avatar asked Oct 19 '10 07:10

šljaker


1 Answers

I attached to the PreRender event:

protected void GridView1_PreRender(object sender, EventArgs e)
{
    var gridView = (GridView) sender;
    var header = (GridViewRow) gridView.Controls[0].Controls[0];

    header.Cells[0].Visible = false;
    header.Cells[1].ColumnSpan = 2;
    header.Cells[1].Text = "Header";
}
like image 134
šljaker Avatar answered Nov 20 '22 11:11

šljaker