<
HTML
>
< HEAD >
< title > MergeHeader </ title >
< meta name ="GENERATOR" Content ="Microsoft Visual Studio .NET 7.1" >
< meta name ="CODE_LANGUAGE" Content ="C#" >
< meta name ="vs_defaultClientScript" content ="JavaScript" >
< meta name ="vs_targetSchema" content ="http://schemas.microsoft.com/intellisense/ie5" >
</ HEAD >
< body MS_POSITIONING ="GridLayout" >
< form id ="Form1" method ="post" runat ="server" >
< asp:datagrid id ="DataGrid1" style ="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" runat ="server"
BorderColor ="Tan" BorderWidth ="1px" BackColor ="LightGoldenrodYellow" CellPadding ="1" ForeColor ="Black"
CellSpacing ="1" >
< FooterStyle BackColor ="Tan" ></ FooterStyle >
< SelectedItemStyle ForeColor ="GhostWhite" BackColor ="DarkSlateBlue" ></ SelectedItemStyle >
< AlternatingItemStyle BackColor ="PaleGoldenrod" ></ AlternatingItemStyle >
< HeaderStyle Font-Bold ="True" BackColor ="Tan" ></ HeaderStyle >
< PagerStyle HorizontalAlign ="Center" ForeColor ="DarkSlateBlue" BackColor ="PaleGoldenrod" ></ PagerStyle >
</ asp:datagrid >
</ form >
</ body >
</ HTML >
< HEAD >
< title > MergeHeader </ title >
< meta name ="GENERATOR" Content ="Microsoft Visual Studio .NET 7.1" >
< meta name ="CODE_LANGUAGE" Content ="C#" >
< meta name ="vs_defaultClientScript" content ="JavaScript" >
< meta name ="vs_targetSchema" content ="http://schemas.microsoft.com/intellisense/ie5" >
</ HEAD >
< body MS_POSITIONING ="GridLayout" >
< form id ="Form1" method ="post" runat ="server" >
< asp:datagrid id ="DataGrid1" style ="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" runat ="server"
BorderColor ="Tan" BorderWidth ="1px" BackColor ="LightGoldenrodYellow" CellPadding ="1" ForeColor ="Black"
CellSpacing ="1" >
< FooterStyle BackColor ="Tan" ></ FooterStyle >
< SelectedItemStyle ForeColor ="GhostWhite" BackColor ="DarkSlateBlue" ></ SelectedItemStyle >
< AlternatingItemStyle BackColor ="PaleGoldenrod" ></ AlternatingItemStyle >
< HeaderStyle Font-Bold ="True" BackColor ="Tan" ></ HeaderStyle >
< PagerStyle HorizontalAlign ="Center" ForeColor ="DarkSlateBlue" BackColor ="PaleGoldenrod" ></ PagerStyle >
</ asp:datagrid >
</ form >
</ body >
</ HTML >
public
class
MergeHeader : System.Web.UI.Page
{
private DataGrid m_dgDatagridToDecorate = null;
private ArrayList m_arrHeaderCells = null;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private Hashtable m_htblRowspanIndex = new Hashtable();
private void Page_Load(object sender, System.EventArgs e)
{
if(!this.IsPostBack)
{
MergeGrid();
}
}
AddMergeHeader#region AddMergeHeader
private void AddMergeHeader(ArrayList arrHeaderCells)
{
m_arrHeaderCells =arrHeaderCells;
}
#endregion
property#region property
private DataGrid DatagridToDecorate
{
get
{
return m_dgDatagridToDecorate;
}
set
{
if(null != m_dgDatagridToDecorate)
{
m_dgDatagridToDecorate.ItemCreated -= new DataGridItemEventHandler(DatagridToDecorate_ItemCreated);
}
m_dgDatagridToDecorate = value;
m_dgDatagridToDecorate.ItemCreated += new DataGridItemEventHandler(DatagridToDecorate_ItemCreated);
}
}
#endregion
DatagridToDecorate_ItemCreated#region DatagridToDecorate_ItemCreated
private void DatagridToDecorate_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
//*** Examine if the item created is the header item
ListItemType lit = e.Item.ItemType;
if(ListItemType.Header == lit)
{
//*** Redirect the default header rendering method to our own method
e.Item.SetRenderMethodDelegate(new RenderMethod(NewRenderMethod));
}
}
#endregion
NewRenderMethod#region NewRenderMethod
private void NewRenderMethod(HtmlTextWriter writer, Control ctl)
{
int iCurrIndex = 0;
for(int i=0; i<m_arrHeaderCells.Count; i++)
{
TableCell item = (TableCell)m_arrHeaderCells[i];
if(item.ColumnSpan > 1)
{
iCurrIndex += item.ColumnSpan-1;
}
if(item.RowSpan > 1)
{
m_htblRowspanIndex.Add(iCurrIndex + i, iCurrIndex + i);
}
item.RenderControl(writer);
}
writer.WriteEndTag("TR");
//*** Add the style attributes that was defined in design time
// to our second row so they both will have the same appearance
m_dgDatagridToDecorate.HeaderStyle.AddAttributesToRender(writer);
//*** Insert the second row
writer.RenderBeginTag("TR");
//*** Render all the cells that was defined in design time, except the last one
// because we already rendered it above
for(int i=0; i< ctl.Controls.Count; i++)
{
if((null == m_htblRowspanIndex[i]))
{
ctl.Controls[i].RenderControl(writer);
}
}
//*** We don't need to write the </TR> close tag because the writer will do that for us
// and so we're done :)
}
#endregion
GetData#region GetData
private DataTable GetData()
{
DataTable dt;
dt = new DataTable();
dt.Columns.Add("Code");
dt.Columns.Add("FName");
dt.Columns.Add("MName");
dt.Columns.Add("LName");
dt.Columns.Add("Age");
dt.Columns.Add("Elementary");
dt.Columns.Add("Junior");
dt.Columns.Add("High");
dt.Columns.Add("Religion");
for(int i=1; i <= 12; i++)
{
DataRow dr = dt.NewRow();
dr["Code"] = "Code" + i.ToString();
dr["FName"] = "FName" + i.ToString();
dr["MName"] = "MName" + i.ToString();
dr["LName"] = "LName" + i.ToString();
dr["Age"] = "Age" + i.ToString();
dr["Elementary"] = "Elementary" + i.ToString();
dr["Junior"] = "Junior" + i.ToString();
dr["High"] = "High" + i.ToString();
dr["Religion"] = "Religion" + i.ToString();
dt.Rows.Add(dr);
}
return dt;
}
#endregion
MergeGrid#region MergeGrid
private void MergeGrid()
{
TableCell cell = null;
DatagridToDecorate = DataGrid1;
ArrayList header = new ArrayList();
cell = new TableCell();
cell.Text = "Name";
cell.RowSpan = 2;
cell.HorizontalAlign = HorizontalAlign.Center;
header.Add(cell);
cell = new TableCell();
cell.Text = "Name";
cell.ColumnSpan = 3;
cell.HorizontalAlign = HorizontalAlign.Center;
header.Add(cell);
cell = new TableCell();
cell.Text = "Age";
cell.RowSpan = 2;
cell.HorizontalAlign = HorizontalAlign.Center;
header.Add(cell);
cell = new TableCell();
cell.Text = "School";
cell.ColumnSpan = 3;
cell.HorizontalAlign = HorizontalAlign.Center;
header.Add(cell);
cell = new TableCell();
cell.Text = "Religion";
cell.RowSpan = 2;
cell.HorizontalAlign = HorizontalAlign.Center;
header.Add(cell);
AddMergeHeader(header);
DataGrid1.DataSource = GetData();
DataGrid1.DataBind();
}
#endregion
Web Form Designer generated code#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
{
private DataGrid m_dgDatagridToDecorate = null;
private ArrayList m_arrHeaderCells = null;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private Hashtable m_htblRowspanIndex = new Hashtable();
private void Page_Load(object sender, System.EventArgs e)
{
if(!this.IsPostBack)
{
MergeGrid();
}
}
AddMergeHeader#region AddMergeHeader
private void AddMergeHeader(ArrayList arrHeaderCells)
{
m_arrHeaderCells =arrHeaderCells;
}
#endregion
property#region property
private DataGrid DatagridToDecorate
{
get
{
return m_dgDatagridToDecorate;
}
set
{
if(null != m_dgDatagridToDecorate)
{
m_dgDatagridToDecorate.ItemCreated -= new DataGridItemEventHandler(DatagridToDecorate_ItemCreated);
}
m_dgDatagridToDecorate = value;
m_dgDatagridToDecorate.ItemCreated += new DataGridItemEventHandler(DatagridToDecorate_ItemCreated);
}
}
#endregion
DatagridToDecorate_ItemCreated#region DatagridToDecorate_ItemCreated
private void DatagridToDecorate_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
//*** Examine if the item created is the header item
ListItemType lit = e.Item.ItemType;
if(ListItemType.Header == lit)
{
//*** Redirect the default header rendering method to our own method
e.Item.SetRenderMethodDelegate(new RenderMethod(NewRenderMethod));
}
}
#endregion
NewRenderMethod#region NewRenderMethod
private void NewRenderMethod(HtmlTextWriter writer, Control ctl)
{
int iCurrIndex = 0;
for(int i=0; i<m_arrHeaderCells.Count; i++)
{
TableCell item = (TableCell)m_arrHeaderCells[i];
if(item.ColumnSpan > 1)
{
iCurrIndex += item.ColumnSpan-1;
}
if(item.RowSpan > 1)
{
m_htblRowspanIndex.Add(iCurrIndex + i, iCurrIndex + i);
}
item.RenderControl(writer);
}
writer.WriteEndTag("TR");
//*** Add the style attributes that was defined in design time
// to our second row so they both will have the same appearance
m_dgDatagridToDecorate.HeaderStyle.AddAttributesToRender(writer);
//*** Insert the second row
writer.RenderBeginTag("TR");
//*** Render all the cells that was defined in design time, except the last one
// because we already rendered it above
for(int i=0; i< ctl.Controls.Count; i++)
{
if((null == m_htblRowspanIndex[i]))
{
ctl.Controls[i].RenderControl(writer);
}
}
//*** We don't need to write the </TR> close tag because the writer will do that for us
// and so we're done :)
}
#endregion
GetData#region GetData
private DataTable GetData()
{
DataTable dt;
dt = new DataTable();
dt.Columns.Add("Code");
dt.Columns.Add("FName");
dt.Columns.Add("MName");
dt.Columns.Add("LName");
dt.Columns.Add("Age");
dt.Columns.Add("Elementary");
dt.Columns.Add("Junior");
dt.Columns.Add("High");
dt.Columns.Add("Religion");
for(int i=1; i <= 12; i++)
{
DataRow dr = dt.NewRow();
dr["Code"] = "Code" + i.ToString();
dr["FName"] = "FName" + i.ToString();
dr["MName"] = "MName" + i.ToString();
dr["LName"] = "LName" + i.ToString();
dr["Age"] = "Age" + i.ToString();
dr["Elementary"] = "Elementary" + i.ToString();
dr["Junior"] = "Junior" + i.ToString();
dr["High"] = "High" + i.ToString();
dr["Religion"] = "Religion" + i.ToString();
dt.Rows.Add(dr);
}
return dt;
}
#endregion
MergeGrid#region MergeGrid
private void MergeGrid()
{
TableCell cell = null;
DatagridToDecorate = DataGrid1;
ArrayList header = new ArrayList();
cell = new TableCell();
cell.Text = "Name";
cell.RowSpan = 2;
cell.HorizontalAlign = HorizontalAlign.Center;
header.Add(cell);
cell = new TableCell();
cell.Text = "Name";
cell.ColumnSpan = 3;
cell.HorizontalAlign = HorizontalAlign.Center;
header.Add(cell);
cell = new TableCell();
cell.Text = "Age";
cell.RowSpan = 2;
cell.HorizontalAlign = HorizontalAlign.Center;
header.Add(cell);
cell = new TableCell();
cell.Text = "School";
cell.ColumnSpan = 3;
cell.HorizontalAlign = HorizontalAlign.Center;
header.Add(cell);
cell = new TableCell();
cell.Text = "Religion";
cell.RowSpan = 2;
cell.HorizontalAlign = HorizontalAlign.Center;
header.Add(cell);
AddMergeHeader(header);
DataGrid1.DataSource = GetData();
DataGrid1.DataBind();
}
#endregion
Web Form Designer generated code#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}