合并DataGrid表头,Merging DataGrid Header Columns

合并DataGrid表头,Merging DataGrid Header Columns



Introduction
this article describes a simple trick to 
merge the header columns of 

a DataGrid at run-time. 
Background
i found a similar solution:
Merge DataGrid Header


but I wanted to do this in 2-3 lines of code. 

So, I tried to use the members of the DataGrid to achieve the results.


 

Using the code
in the ItemCreated event of the DataGrid, catch the header item of the DataGrid. Suppose you want to merge n columns (cells of the header row), then remove n-1 cells from the header item. Then, make the column span property of the remaining cell equal to n. Then, add a table to the cell according to your requirements. The code below demonstrates the merging of two columns, which will result as shown in the above diagram. 
C#private void  Sub Datagrid1_ItemCreated(object sender,
        System.Web.UI.WebControls.DataGridItemEventArgs e )
{
    //If Header item
   
    If (e.Item.ItemType = = ListItemType.Header)
    {
        e.Item.Cells.RemoveAt(2);
        e.Item.Cells(1).ColumnSpan = 2;
        //Insert the table shown in the diagram 3
        // to the Text property of the Cell
        e.Item.Cells(1).Text = " 
              " =center>Name" 
              "F NameL"  " Name";
    } 
}
VB.NETPrivate Sub Datagrid1_ItemCreated(ByVal sender As _
        Object, ByVal e System.Web.UI.WebControls.DataGridItemEventArgs)_
        Handles Datagrid1_ItemCreated
    'If Header item
   
    If e.Item.ItemType = ListItemType.Header Then
      e.Item.Cells.RemoveAt(2)
      e.Item.Cells(1).ColumnSpan = 2
      'Insert the table shown in the diagram 3 to the Text property of the Cell
      e.Item.Cells(1).Text = "&;amp; _
        " align =center>Name" &;amp; _
        "F NameL" &;amp; _ " Name"
    End If 
End Sub
Jayaprakash V






Click
here
to view Jayaprakash V's online profile.    (2006-4-25:09:28)

 感谢原创者的辛勤劳动,希望对您有所帮助,转载请注明原出处。
 您可能对 [Asp.Net] 的这些文章也感兴趣:

ASP.NET2.0 SQLServer2005构建多层应用
为按钮添加 确认 对话框
Asp.Net Mvc Beta新特性之自动绑定(1)-体验篇
单点登录SSO(Single Sign-On for everyone)解决方案大全
ASP.NET热点问题解答14个
ASP.NET中用户输入文本的HTML解析办法
在ASP.NET中上传图片并生成缩略图
ASP.NET 2.0:AdventureWorks系统分析
ASP.NET2.0和数据绑定控件:新的角度,新的做法
在ASP.NET中如何用C#.NET实现基于表单的验证