Sunday, September 17, 2017

// // 2 comments

How to merge Gridview header cells C#


In this article I am going to explain How to merge Gridview header cells with C#

As I explained in previous articles bind data with Gridview, If you are new to this article series, please visit How to bind data with Gridview

After bind data Gridview headings shows as below

|    #     |        Name        |     Code    |

I am going to inset an another row above to this headings as below

|  Cheque |                 Bank                  |
----------------------------------------------
|      #       |        Name        |     Code    |


Cheque header column is one cell and Bank header as with 2 cells merged.

Use the below mentioned code inside the RoeDataBound event.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

 GridViewRow gvr = e.Row;

        if (gvr.RowType == DataControlRowType.Header)
        {
            GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);

            TableCell cell = new TableCell();
            cell.ColumnSpan = 1;
            cell.HorizontalAlign = HorizontalAlign.Center;
            cell.Text = "Cheque";
            row.Cells.Add(cell);

            cell = new TableCell();
            cell.ColumnSpan = 2;
            cell.HorizontalAlign = HorizontalAlign.Center;
            cell.Text = "Bank";
            row.Cells.Add(cell);

            GridView1.Controls[0].Controls.AddAt(0, row);
        }
}


Please watch video for more details.




2 comments:

  1. In column NAME, you´ve merged rows, is it possible to show us how to do this ? :D

    thanks!

    ReplyDelete
  2. please visit video, you can understand how to do this

    ReplyDelete