views:

30

answers:

3

Hi everyone!

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!

A: 

Take a look at: http://aspnet.4guysfromrolla.com/articles/072603-1.aspx
I think that's what you're looking for.
UPDATE: Found some other sources.
http://netindonesia.net/blogs/irwansyah/archive/2007/06/05/Merge-GridView-Header.aspx
http://netindonesia.net/blogs/irwansyah/archive/2007/06/05/Merge-GridView-Header.aspx
This post explains exactly what you're trying to achieve. (and more)

Kamyar
A: 

Following code help you to

  protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {

            //Build custom header.
            GridView oGridView = (GridView)sender;
            GridViewRow oGridViewRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);


            TableHeaderCell oTableCell = new TableHeaderCell();
            oTableCell.Text = "Agent Id/ Outcome";
            oTableCell.ColumnSpan = 1;
            oGridViewRow.Cells.Add(oTableCell);


            for (int j = 0; j < totalOutcome; j++)
            {
                oTableCell = new TableHeaderCell();

                oTableCell.Text = dsOutcome.Tables[0].Rows[j]["outcome"].ToString();
                oTableCell.ColumnSpan = 2;
                oGridViewRow.Cells.Add(oTableCell);
            }
            oGridView.Controls[0].Controls.AddAt(0, oGridViewRow);

            for (int i = 0; i < e.Row.Cells.Count; i++)
            {
                if (i == 0)
                    e.Row.Cells[i].Text = "";
                else if (i % 2 == 0)
                    e.Row.Cells[i].Text = "%";
                else
                    e.Row.Cells[i].Text = "Calls";
            }
        }
    }
Pranay Rana
A: 

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";
}
šljaker