Shows how to merge DataGrid header at run-time.

DataGrid表头合并技巧
本文介绍了一种通过重定向ASP.NET中DataGrid控件的渲染方法来实现表头合并的技术。通过覆盖默认的渲染过程,可以灵活地控制DataGrid表头的呈现方式,实现跨列或跨行的合并效果。

Download

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:

None.gif private   void  Datagrid1_ItemCreated( object  sender, 
None.gif          System.Web.UI.WebControls.DataGridItemEventArgs e) 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif    
//*** Examine if the item created is the header item 
InBlock.gif
    ListItemType lit = e.Item.ItemType; 
InBlock.gif    
if(ListItemType.Header == lit) 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif        
//*** Redirect the default header rendering method to our own method 
InBlock.gif
        e.Item.SetRenderMethodDelegate(new RenderMethod(NewRenderMethod)); 
ExpandedSubBlockEnd.gif    }
 
ExpandedBlockEnd.gif}
And here is our own Render method:
ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary> 
InBlock.gif
/// This is our custom render method for the grid header item 
InBlock.gif
/// </summary> 
InBlock.gif
/// <param name="writer"></param> 
ExpandedBlockEnd.gif
/// <param name="ctl"></param> 

None.gif private   void  NewRenderMethod(HtmlTextWriter writer, Control ctl) 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif    
//***  We don't need to write the <TR> tag
InBlock.gif    
//     because it's already written by the writer 
InBlock.gif    
//     so now we write the Name column 
InBlock.gif
    writer.Write("<TD colspan=\"3\" align=\"center\">Name</TD>\n"); 
InBlock.gif
InBlock.gif    
//***  The Age column must have the rowspan attribute
InBlock.gif    
//     and must be rendered inside the 
InBlock.gif    
//     first row so it will centered vertically 
InBlock.gif
    TableCell cell = (TableCell)ctl.Controls[ctl.Controls.Count-1]; 
InBlock.gif    cell.Attributes.Add(
"rowspan","2"); 
InBlock.gif    cell.RenderControl(writer); 
InBlock.gif
InBlock.gif    
//***     Now we close the first row, so we can insert the second one 
InBlock.gif
    writer.Write("</TR>\n"); 
InBlock.gif
InBlock.gif    
//***  Add the style attributes that was defined in design time 
InBlock.gif    
//     to our second row so they both will have the same appearance 
InBlock.gif
    DataGrid1.HeaderStyle.AddAttributesToRender(writer); 
InBlock.gif
InBlock.gif    
//***     Insert the second row 
InBlock.gif
    writer.RenderBeginTag("TR"); 
InBlock.gif
InBlock.gif    
//***  Render all the cells that was defined
InBlock.gif    
//     in design time, except the last one 
InBlock.gif    
//     because we already rendered it above 
InBlock.gif
    for(int i=0; i<= ctl.Controls.Count-2; i++
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif        ctl.Controls[i].RenderControl(writer); 
ExpandedSubBlockEnd.gif    }
 
InBlock.gif
InBlock.gif    
//***  We don't need to write the </TR> close tag
InBlock.gif    
//     because the writer will do that for us 
InBlock.gif    
//     and so we're done :) 
ExpandedBlockEnd.gif
}
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):
None.gif private   void  Page_Load( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
// Put user code to initialize the page here
InBlock.gif
    if(!this.IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        TableCell cell 
= null;
InBlock.gif        DataGrid1.DataSource 
= GetData();
InBlock.gif        DataGrid1.DataBind(); 
InBlock.gif
InBlock.gif        m_add.DatagridToDecorate 
= Datagrid2;
InBlock.gif        ArrayList header 
= new ArrayList();
InBlock.gif
InBlock.gif        
// cell = new TableCell();
InBlock.gif        
// cell.Text = "Code";
InBlock.gif        
// cell.RowSpan = 2;
InBlock.gif        
// cell.HorizontalAlign = HorizontalAlign.Center;
InBlock.gif        
// header.Add(cell);
InBlock.gif

InBlock.gif        cell 
= new TableCell();
InBlock.gif        cell.Text 
= "Name";
InBlock.gif        cell.RowSpan 
= 2;
InBlock.gif        cell.HorizontalAlign 
= HorizontalAlign.Center;
InBlock.gif        header.Add(cell);
InBlock.gif
InBlock.gif        cell 
= new TableCell();
InBlock.gif        cell.Text 
= "Name";
InBlock.gif        cell.ColumnSpan 
= 3;
InBlock.gif        cell.HorizontalAlign 
= HorizontalAlign.Center;
InBlock.gif        header.Add(cell);
InBlock.gif
InBlock.gif        cell 
= new TableCell();
InBlock.gif        cell.Text 
= "Age";
InBlock.gif        cell.RowSpan 
= 2;
InBlock.gif        cell.HorizontalAlign 
= HorizontalAlign.Center;
InBlock.gif        header.Add(cell);
InBlock.gif
InBlock.gif        cell 
= new TableCell();
InBlock.gif        cell.Text 
= "School";
InBlock.gif        cell.ColumnSpan 
= 3;
InBlock.gif        cell.HorizontalAlign 
= HorizontalAlign.Center;
InBlock.gif        header.Add(cell);
InBlock.gif
InBlock.gif        cell 
= new TableCell();
InBlock.gif        cell.Text 
= "Religion";
InBlock.gif        cell.RowSpan 
= 2;
InBlock.gif        cell.HorizontalAlign 
= HorizontalAlign.Center;
InBlock.gif        header.Add(cell);
InBlock.gif
InBlock.gif        m_add.AddMergeHeader(header);
InBlock.gif
InBlock.gif        Datagrid2.DataSource 
= GetData();
InBlock.gif        Datagrid2.DataBind();
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/ryb/archive/2006/03/29/361439.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值