Monday, December 19, 2011

Merging Gridview with Multiple Headers

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page 
{
    #region Declarations
    CustomersDataObject objData = null;
    GridViewRow HeaderRow = null;
    #endregion

    #region Events
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            objData = new CustomersDataObject();
            //DataTable dt =  objData.Select().Table ;
            gvStylish.DataSource = objData.Select();
            gvStylish.DataBind();
        }
    }

    protected void gvStylish_RowCreated(object sender, GridViewRowEventArgs e)
    {
        //http://www.codeproject.com/Articles/249155/Rows-and-Columns-Merging-in-ASP-NET-GridView-Contr
        //http://sammisoft.net/mboard/mboard/mboard.asp?exe=view&board_id=pds&group_name=sammisoft&idx_num=194&page=22&category=0&search_category=&search_word=&order_c=intVote&order_da=asc
        //http://www.thaicreate.com/dotnet/forum/048135.html

        if (e.Row.RowType == DataControlRowType.Header)
        {
            GridView HeaderGrid = (GridView)sender;
            HeaderRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
            for (int cellCount = 0; cellCount < e.Row.Cells.Count; cellCount++)
            {
                if (e.Row.Cells[cellCount].Text.Trim() == "CustomerID"
                    || e.Row.Cells[cellCount].Text.Trim() == "CompanyName"
                    || e.Row.Cells[cellCount].Text.Trim() == "ContactName")
                {
                    //Row Span
                    HeaderRow.Cells.Add(HeaderRowMerg(e, 2, cellCount, System.Drawing.Color.Gray.Name));
                }

                else
                {
                    //Col Span
                    HeaderRow.Cells.Add(HeaderColumnMerg(e, 3, "Subjects", System.Drawing.Color.Gray.Name));
                    break;
                }

            }
            gvStylish.Controls[0].Controls.AddAt(0, HeaderRow);
        }
    }

    /// 
    /// Merging datarow cells
    /// 
    /// /// protected void gvStylish_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            /*Merging starts*/
            int _iNoOfCellsToCheck = 6;
            for (int rowIndex = gvStylish.Rows.Count - 2; rowIndex >= 0; rowIndex--)
            {
                GridViewRow gvRow = gvStylish.Rows[rowIndex];
                GridViewRow gvPreviousRow = gvStylish.Rows[rowIndex + 1];
                //for (int cellCount = 0; cellCount < gvRow.Cells.Count; cellCount++)
                for (int cellCount = 1; cellCount < _iNoOfCellsToCheck; cellCount++)
                {
                    if (gvRow.Cells[cellCount].Text == gvPreviousRow.Cells[cellCount].Text)
                    {
                        gvRow.Cells[cellCount].RowSpan =
                            gvPreviousRow.Cells[cellCount].RowSpan < 2 ?
                            2 :
                            gvPreviousRow.Cells[cellCount].RowSpan + 1;

                        gvPreviousRow.Cells[cellCount].Visible = false;
                    }
                }
            }
            /*Merging ends*/

            //If we want to set background color for the row
            //if (e.Row.Cells[0].Text.Trim().ToUpper().Contains("TOTAL"))
            if (e.Row.Cells[0].Text.Trim().ToUpper().Contains("MUTHU"))
            {

                SetRowBackColor(e, System.Drawing.Color.Silver.Name);
            }
            else
                //if (e.Row.Cells[0].Text.Trim().ToUpper().Contains("GRAND"))
                if (e.Row.Cells[0].Text.Trim().ToUpper().Contains("VIJAY"))
                {
                    SetRowBackColor(e, "#999999");
                }
        }
    }
    #endregion

    #region Functions
    private TableCell HeaderRowMerg(GridViewRowEventArgs e, int iRowSpan, int iIndex, string backcolor)
    {
        TableHeaderCell Cell_Header = new TableHeaderCell();
        Cell_Header.Text = e.Row.Cells[iIndex].Text;
        Cell_Header.HorizontalAlign = HorizontalAlign.Center;
        Cell_Header.Font.Bold = true;
        Cell_Header.RowSpan = iRowSpan;
        Cell_Header.Style.Add("background-color", backcolor);
        e.Row.Cells[iIndex].Attributes.Add("style", "display: none;");
        return Cell_Header;
    }

    private TableCell HeaderColumnMerg(GridViewRowEventArgs e, int iColumnSpan, string _strHeaderText, string backcolor)
    {
        TableHeaderCell cell = new TableHeaderCell();
        Cell_Header.Text = _strHeaderText;
        Cell_Header.HorizontalAlign = HorizontalAlign.Center;
        Cell_Header.ColumnSpan = iColumnSpan;
        Cell_Header.Font.Bold = true;
        Cell_Header.Style.Add("background-color", backcolor);
        return Cell_Header;
    }

    bool IsInt(string strVal)
    {
        try
        {
            int value = int.Parse(strVal);
            return true;
        }
        catch
        {
            return false;
        }
    }

    private void SetRowBackColor(GridViewRowEventArgs e, string color)
    {
        e.Row.BackColor = System.Drawing.Color.FromName(color);
        e.Row.Font.Bold = true;
        foreach (TableCell tc in e.Row.Cells)
        {
            if (IsInt(tc.Text))
                tc.Text = Convert.ToInt32(tc.Text).ToString("#,#");
        }
    }
    #endregion
}