Wednesday 29 August 2012

gridview cell merge options


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

No comments:

Post a Comment

Contact Us:

Email:

Vinodkumar434@gmail.com,
vinodtechnosoft@gmail.com

Skype Name:

vinodtechnosoft