Merge DataGrid Header
来源:百度文库 编辑:神马文学网 时间:2024/05/01 20:05:28
Introduction
This article describes the technique to merge the header of a DataGrid by redirecting the Render method of the DataGrid items.
Background
I found many times the need to merge the header of a DataGrid. So, when I was having spare time, I tried to find a way to do it, and here it is. I know that if you need to merge headers, you can use the Repeater control instead. But if you are fond of DataGrid (just like me), or may be you already used DataGrid, then this article is for you.
Using the code
When rendered, a DataGrid will be converted into a HTML Table element and the header will be the first HTML TR element. So, to have a merged header, we need to have control in the rendering of the header. This can be achieved by redirecting the Render method of the DataGrid header using the SetRenderMethodDelegate of the DataGrid header on ItemCreated event, like this:
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
////// This is our custom render method for the grid header item/// /// /// private void NewRenderMethod(HtmlTextWriter writer, Control ctl){//*** We don‘t need to write the tag // because it‘s already written by the writer // so now we write the Name column writer.Write("Name \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(" \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 close tag // because the writer will do that for us // and so we‘re done :)}
I have created a decorator class to decorate a DataGrid (ASPNetDatagridDecorator class) to have a merge header, and all you need to do is define the header cell like this (you can use the auto format feature, but doesn‘t work for all):
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();}}
This article describes the technique to merge the header of a DataGrid by redirecting the Render method of the DataGrid items.
Background
I found many times the need to merge the header of a DataGrid. So, when I was having spare time, I tried to find a way to do it, and here it is. I know that if you need to merge headers, you can use the Repeater control instead. But if you are fond of DataGrid (just like me), or may be you already used DataGrid, then this article is for you.
Using the code
When rendered, a DataGrid will be converted into a HTML Table element and the header will be the first HTML TR element. So, to have a merged header, we need to have control in the rendering of the header. This can be achieved by redirecting the Render method of the DataGrid header using the SetRenderMethodDelegate of the DataGrid header on ItemCreated event, like this:
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
///
I have created a decorator class to decorate a DataGrid (ASPNetDatagridDecorator class) to have a merge header, and all you need to do is define the header cell like this (you can use the auto format feature, but doesn‘t work for all):
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();}}
Merge DataGrid Header
Virtual Grid Control(Merge Header)
DataGrid大全
HTTP的Header信息
Flex 2 DataGrid 分页
Flex 2 DataGrid 分页
GridView header on 2 column
HTTP协议header头域
GridView header on 2 column
C Header File Include Patterns
强大的DataGrid组件[7]_自定义DataGrid
VB--DataGrid 控件的使用
GridView、DataGrid、DataList、Repeater、ListView、...
DataGrid/DataList的使用AA
bytea - "Cannot modify header information"的解决方法
关于 Cannot modify header information 的解决
HTTP的一些参考资料和Header信息
Exceed PHP - header 函式的使用
WordPress 主题教程 #4a:Header 模板
header中的Cache-control参数说明
转贴:Cannot open precompiled header file:'Debug...
oracle10g的merge功能增强(转)
【转】 , , and
VB--DataGrid 控件的使用1