IntroductionThis article describes the technique to merge the header of a BackgroundI found many times the need to merge the header of a Using the codeWhen rendered, a private void Datagrid1_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));
}
}
And here is our own Render method: Collapse
/// <summary>
/// This is our custom render method for the grid header item
/// </summary>
/// <param name="writer"></param>
/// <param name="ctl"></param>
private void NewRenderMethod(HtmlTextWriter writer, Control ctl)
{
//*** We don‘t need to write the <TR> tag
// because it‘s already written by the writer
// so now we write the Name column
writer.Write("<TD colspan=\"3\" align=\"center\">Name</TD>\n");
//*** The Age column must have the rowspan attribute
// and must be rendered inside the
// first row so it will centered vertically
TableCell cell = (TableCell)ctl.Controls[ctl.Controls.Count-1];
cell.Attributes.Add("rowspan","2");
cell.RenderControl(writer);
//*** Now we close the first row, so we can insert the second one
writer.Write("</TR>\n");
//*** Add the style attributes that was defined in design time
// to our second row so they both will have the same appearance
DataGrid1.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-2; 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 :)
}
I have created a decorator class to decorate a Collapse
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if(!this.IsPostBack)
{
TableCell cell = null;
DataGrid1.DataSource = GetData();
DataGrid1.DataBind();
m_add.DatagridToDecorate = Datagrid2;
ArrayList header = new ArrayList();
// cell = new TableCell();
// cell.Text = "Code";
// cell.RowSpan = 2;
// cell.HorizontalAlign = HorizontalAlign.Center;
// header.Add(cell);
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);
m_add.AddMergeHeader(header);
Datagrid2.DataSource = GetData();
Datagrid2.DataBind();
}
}
|
|
来自: louisasea > 《Develope》