.NET Discussion

.NET Issues, Problems, Code Samples, and Fixes

ASP.NET Gridview: Merging Cells


I had been searching for a way to merge cells in a GridView, and recently the answer was given to me by Mike Flavin (thanks!). 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 Then
     e.Row.Cells(0).RowSpan = 2
     e.Row.Cells(1).RowSpan = 2
End If
'Remove the extra cells created due to row span for odd rows
If e.Row.DataItemIndex % 2 = 1 Then
     e.Row.Cells.RemoveAt(0)
     e.Row.Cells.RemoveAt(0)
     e.Row.Cells(1).HorizontalAlign = HorizontalAlign.Center
End If

This code should take every cell in the first and second columns and merge it with the cell directly below it. Of course, this should be used very carefully, because you could screw up your data if each record does not have another corresponding record with it. However, this shouldn’t be too difficult to program around by adding data checks.

Advertisements

November 15, 2007 - Posted by | ASP.NET, GridView, Tips & Tricks

5 Comments »

  1. Doesn’t work as it says,
    (I am using VS 2005 GridView for ASP.Net)

    Removes the cells as you said but shifts the entire column to the left.

    Any Ideas?

    Nhilesh Baua

    Comment by Nhilesh Baua | December 20, 2007 | Reply

  2. Hi…….
    I want to know that can we delete entire column from gridview in asp.net and how?? I am using visual studio 2005

    Comment by devi | April 10, 2008 | Reply

  3. Devi –

    Delete, it’s possible I suppose… just access your Columns collection and I believe there’s a delete method there. If not, you can always set the .Visible property to False. I do that to my Edit/Save/Cancel button column when a non-Admin user is accessing my prices.

    Comment by Some.Net(Guy) | April 10, 2008 | Reply

  4. string previousCat = “”;
    int firstRow = 3;
    protected void gvUsers_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    //if (e.Row.RowType == DataControlRowType.DataRow)
    //{
    // DataRowView drv = ((DataRowView)e.Row.DataItem);

    // if (statename == drv[“State”].ToString())
    // {

    // if (gvUsers.Rows[thirdrow].Cells[2].RowSpan == 0)

    // gvUsers.Rows[thirdrow].Cells[2].RowSpan = 2;

    // else
    // {
    // gvUsers.Rows[thirdrow].Cells[2].RowSpan += 1;

    // }
    // e.Row.Cells.RemoveAt(2);

    // //e.Row.Cells[2].Visible = false;

    // }

    // else
    // {
    // e.Row.VerticalAlign = VerticalAlign.Top;

    // statename = drv[“State”].ToString();

    // thirdrow = e.Row.RowIndex;
    // }

    //}

    //if (e.Row.RowType == DataControlRowType.DataRow)
    //{

    // //We’re only interested in Rows that contain data
    // //get a reference to the data used to databound the row
    // DataRowView drv = ((DataRowView)e.Row.DataItem);

    // if (previousCat == drv[“State”].ToString())
    // {

    // //If it’s the same category as the previous one
    // //Increment the rowspan
    // if (gvUsers.Rows[firstRow].Cells[2].RowSpan == 0)

    // gvUsers.Rows[firstRow].Cells[2].RowSpan = 2;

    // else
    // gvUsers.Rows[firstRow].Cells[2].RowSpan += 1;

    // //Remove the cell
    // e.Row.Cells.RemoveAt(0);

    // }

    // else //It’s a new category
    // {

    // //Set the vertical alignment to top
    // e.Row.VerticalAlign = VerticalAlign.Top;

    // //Maintain the category in memory
    // previousCat = drv[“State”].ToString();

    // firstRow = e.Row.RowIndex;

    // }

    //}
    }

    i m using this code for mergig columnn cells which have same name but when i goes on another page and if state name is same is shows a error out of error.if state name is changed then it not show any error ang go on next page. in this code in 2to 9 page its give error and on 10th page no error. give me solution of this.
    plz
    thanks

    Comment by ALOK | March 20, 2009 | Reply

  5. Row span can be coded in Gridview rowcreated event also

    Comment by GridView | June 1, 2009 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: