Merge Data Columns in Gridview

Prabhakar
Posted by in ASP.NET category on for Intermediate level | Points: 250 | Views : 17915 red flag
Rating: 5 out of 5  
 1 vote(s)

In this article write the code to merge column & cells in same data in cells. I am described how to merge cells in Gridview in ASP.NET . In this article I am add OnRowDataBound Event of gridview and use RowSpan of cells.

In this article I am connecting with sql database & in a table I have some columns like college description, college name with branch wise.. both of colleges have many branches so I merge college name Like this


 

on Aspx page

In aspx page write Gridview Control then add SqldataSource . Configure database with collge table and fill grid view. All data show in gridview. Add event in gridview  OnRowDataBound="GridView1_RowDataBound"

 

 

In aspx.cs page write this code

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

    {

        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["Collegename"].ToString())

            {

                //If it's the same category as the previous one

                //Increment the rowspan

                if (GridView1.Rows[firstRow].Cells[0].RowSpan == 0)

 

                    GridView1.Rows[firstRow].Cells[0].RowSpan = 2;

                else

                    GridView1.Rows[firstRow].Cells[0].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["Collegename "].ToString();

                firstRow = e.Row.RowIndex;

            }

        }

    }

Then you run your page & Look like Merger data in a Gridview

Conclusion

In this article I have tried to solve the problem to merge repeat columns in gridview. Hope fully it helps someone.


Prabhakar

Page copy protected against web site content infringement by Copyscape

About the Author

Prabhakar
Full Name: prabhakar parihar
Member Level:
Member Status: Member,MVP
Member Since: 1/12/2011 5:05:40 AM
Country: India
Best Regard's Prabhakar
http://www.dotnetfunda.com/profile/prabhakar.aspx
7.6 Years exp. Software Development with ASP.NET C#

Login to vote for this post.

Comments or Responses

Posted by: Sushilkumar.sush on: 10/28/2011 | Points: 25
hi,
This article helpful but need some some more help .
I need the first column merge in gridview which is u show above ,but i have templates field so i did not able to merge it ..
How can i merge the same colume name using the templates filed..

Thanks

Sushil shinde

Login to post response

Comment using Facebook(Author doesn't get notification)