Monday, December 19, 2011

Merging Gridview with Multiple Headers

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Web;  
  4. using System.Web.UI;  
  5. using System.Web.UI.WebControls;  
  6.   
  7. public partial class _Default : System.Web.UI.Page   
  8. {  
  9.     #region Declarations  
  10.     CustomersDataObject objData = null;  
  11.     GridViewRow HeaderRow = null;  
  12.     #endregion  
  13.  
  14.     #region Events  
  15.     protected void Page_Load(object sender, EventArgs e)  
  16.     {  
  17.         if (!IsPostBack)  
  18.         {  
  19.             objData = new CustomersDataObject();  
  20.             //DataTable dt =  objData.Select().Table ;  
  21.             gvStylish.DataSource = objData.Select();  
  22.             gvStylish.DataBind();  
  23.         }  
  24.     }  
  25.   
  26.     protected void gvStylish_RowCreated(object sender, GridViewRowEventArgs e)  
  27.     {  
  28.         //http://www.codeproject.com/Articles/249155/Rows-and-Columns-Merging-in-ASP-NET-GridView-Contr  
  29.         //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  
  30.         //http://www.thaicreate.com/dotnet/forum/048135.html  
  31.   
  32.         if (e.Row.RowType == DataControlRowType.Header)  
  33.         {  
  34.             GridView HeaderGrid = (GridView)sender;  
  35.             HeaderRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);  
  36.             for (int cellCount = 0; cellCount < e.Row.Cells.Count; cellCount++)  
  37.             {  
  38.                 if (e.Row.Cells[cellCount].Text.Trim() == "CustomerID"  
  39.                     || e.Row.Cells[cellCount].Text.Trim() == "CompanyName"  
  40.                     || e.Row.Cells[cellCount].Text.Trim() == "ContactName")  
  41.                 {  
  42.                     //Row Span  
  43.                     HeaderRow.Cells.Add(HeaderRowMerg(e, 2, cellCount, System.Drawing.Color.Gray.Name));  
  44.                 }  
  45.   
  46.                 else  
  47.                 {  
  48.                     //Col Span  
  49.                     HeaderRow.Cells.Add(HeaderColumnMerg(e, 3, "Subjects", System.Drawing.Color.Gray.Name));  
  50.                     break;  
  51.                 }  
  52.   
  53.             }  
  54.             gvStylish.Controls[0].Controls.AddAt(0, HeaderRow);  
  55.         }  
  56.     }  
  57.   
  58.     /// <summary>  
  59.     /// Merging datarow cells  
  60.     /// </summary>  
  61.     /// <param name="sender">/// <param name="e">protected void gvStylish_RowDataBound(object sender, GridViewRowEventArgs e)  
  62.     {  
  63.         if (e.Row.RowType == DataControlRowType.DataRow)  
  64.         {  
  65.             /*Merging starts*/  
  66.             int _iNoOfCellsToCheck = 6;  
  67.             for (int rowIndex = gvStylish.Rows.Count - 2; rowIndex >= 0; rowIndex--)  
  68.             {  
  69.                 GridViewRow gvRow = gvStylish.Rows[rowIndex];  
  70.                 GridViewRow gvPreviousRow = gvStylish.Rows[rowIndex + 1];  
  71.                 //for (int cellCount = 0; cellCount < gvRow.Cells.Count; cellCount++)  
  72.                 for (int cellCount = 1; cellCount < _iNoOfCellsToCheck; cellCount++)  
  73.                 {  
  74.                     if (gvRow.Cells[cellCount].Text == gvPreviousRow.Cells[cellCount].Text)  
  75.                     {  
  76.                         gvRow.Cells[cellCount].RowSpan =  
  77.                             gvPreviousRow.Cells[cellCount].RowSpan < 2 ?  
  78.                             2 :  
  79.                             gvPreviousRow.Cells[cellCount].RowSpan + 1;  
  80.   
  81.                         gvPreviousRow.Cells[cellCount].Visible = false;  
  82.                     }  
  83.                 }  
  84.             }  
  85.             /*Merging ends*/  
  86.   
  87.             //If we want to set background color for the row  
  88.             //if (e.Row.Cells[0].Text.Trim().ToUpper().Contains("TOTAL"))  
  89.             if (e.Row.Cells[0].Text.Trim().ToUpper().Contains("MUTHU"))  
  90.             {  
  91.   
  92.                 SetRowBackColor(e, System.Drawing.Color.Silver.Name);  
  93.             }  
  94.             else  
  95.                 //if (e.Row.Cells[0].Text.Trim().ToUpper().Contains("GRAND"))  
  96.                 if (e.Row.Cells[0].Text.Trim().ToUpper().Contains("VIJAY"))  
  97.                 {  
  98.                     SetRowBackColor(e, "#999999");  
  99.                 }  
  100.         }  
  101.     }  
  102.     #endregion  
  103.  
  104.     #region Functions  
  105.     private TableCell HeaderRowMerg(GridViewRowEventArgs e, int iRowSpan, int iIndex, string backcolor)  
  106.     {  
  107.         TableHeaderCell Cell_Header = new TableHeaderCell();  
  108.         Cell_Header.Text = e.Row.Cells[iIndex].Text;  
  109.         Cell_Header.HorizontalAlign = HorizontalAlign.Center;  
  110.         Cell_Header.Font.Bold = true;  
  111.         Cell_Header.RowSpan = iRowSpan;  
  112.         Cell_Header.Style.Add("background-color", backcolor);  
  113.         e.Row.Cells[iIndex].Attributes.Add("style""display: none;");  
  114.         return Cell_Header;  
  115.     }  
  116.   
  117.     private TableCell HeaderColumnMerg(GridViewRowEventArgs e, int iColumnSpan, string _strHeaderText, string backcolor)  
  118.     {  
  119.         TableHeaderCell cell = new TableHeaderCell();  
  120.         Cell_Header.Text = _strHeaderText;  
  121.         Cell_Header.HorizontalAlign = HorizontalAlign.Center;  
  122.         Cell_Header.ColumnSpan = iColumnSpan;  
  123.         Cell_Header.Font.Bold = true;  
  124.         Cell_Header.Style.Add("background-color", backcolor);  
  125.         return Cell_Header;  
  126.     }  
  127.   
  128.     bool IsInt(string strVal)  
  129.     {  
  130.         try  
  131.         {  
  132.             int value = int.Parse(strVal);  
  133.             return true;  
  134.         }  
  135.         catch  
  136.         {  
  137.             return false;  
  138.         }  
  139.     }  
  140.   
  141.     private void SetRowBackColor(GridViewRowEventArgs e, string color)  
  142.     {  
  143.         e.Row.BackColor = System.Drawing.Color.FromName(color);  
  144.         e.Row.Font.Bold = true;  
  145.         foreach (TableCell tc in e.Row.Cells)  
  146.         {  
  147.             if (IsInt(tc.Text))  
  148.                 tc.Text = Convert.ToInt32(tc.Text).ToString("#,#");  
  149.         }  
  150.     }  
  151.     #endregion  
  152. }  

No comments: