How To Merge GridView Cells

Place the following code, or some variation thereof depending on your requirements,
in the RowBinding event of your GridView:

if(e.Row.DataItemIndex%2=0)
{
e.Row.Cells[0].RowSpan = 2;
e.Row.Cells[1].RowSpan = 2;


}

'Remove the extra cells created due to row span for odd rows
if(e.Row.DataItemIndex %2=1)
{
e.Row.Cells.RemoveAt[0];
e.Row.Cells.RemoveAt[0];
e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Center;


}
Reactions

Post a Comment

1 Comments

  1. In my case, I had to use this way (because I needed merge the Header of the Grid)

    protected void Page_Load(object sender, EventArgs e)

    {

    DataTable dt = new DataTable();

    dt.Columns.Add(new DataColumn("id_noticia"));

    dt.Columns.Add(new DataColumn("desc_noticia"));

    dt.Columns.Add(new DataColumn("url_noticia"));

    DataRow dr0 = dt.NewRow();

    dr0["id_noticia"] = "Id da Notícia";

    dr0["desc_noticia"] = "Descrição";

    dr0["url_noticia"] = "url";

    dt.Rows.Add(dr0);

    DataRow dr = dt.NewRow();

    dr["id_noticia"] = "1";

    dr["desc_noticia"] = "teste de notícia interno";

    dr["url_noticia"] = "Detalhes.aspx?id=";

    dt.Rows.Add(dr);

    DataRow dr2 = dt.NewRow();

    dr2["id_noticia"] = "";

    dr2["desc_noticia"] = "teste de notícia externo";

    dr2["url_noticia"] = "http://www.google.com.br";

    dt.Rows.Add(dr2);

    GridView1.DataSource = dt;

    GridView1.DataBind();

    //this is the "key"
    GridView1.HeaderRow.Cells[2].ColumnSpan = 2;

    GridView1.HeaderRow.Cells.RemoveAt(3);

    }

    //***************************
    //****By Edilson rastnews@hotmail.com
    //****From Brazil
    //***************************

    ReplyDelete

Please do not post any spam link in the comment box😊

Emoji
(y)
:)
:(
hihi
:-)
:D
=D
:-d
;(
;-(
@-)
:P
:o
:>)
(o)
:p
(p)
:-s
(m)
8-)
:-t
:-b
b-(
:-#
=p~
x-)
(k)

Close Menu