[转帖]DataGrid显示双层表头,即可实现合并单元格问题

http://xml.sz.luohuedu.net/xml/ShowDetail.asp?id=B3F3462D-DC34-41CE-9FEE-6965B2A3D1AD
假设你的DataGrid有三列,现在想将前两列作为"大类1",第三列作为"大类2",现在,你可以在ItemDataBound事件中加入下面的代码:
if (e.Item.ItemType == ListItemType.Header)
{
     e.Item.Cells[0].ColumnSpan = 2;
     e.Item.Cells[0].Text = "大类1</td><td>大类2</td></tr><tr><td>" + e.Item.Cells[0].Text;
}
用这个方法可以为任意添加新行。
 C#
if (e.Item.ItemType == ListItemType.Header)
                 {
                       DataGridItem dgi = new DataGridItem(0,-1,ListItemType.Header);
                       DataGridItem dgi1= new DataGridItem(0,-1,ListItemType.Header);
                       Table tb = new Table();
                       tb = (Table)DataGrid1.Controls[0];
                       tb.Rows.AddAt(0,dgi);
                       tb.Rows.AddAt(1,dgi1);
                       TableCell tc = new TableCell();
                       TableCell tc1 = new TableCell();
                       TableCell tc2 = new TableCell();
                       TableCell tc22 = new TableCell();
                       tc.Text = "ddd";
                       tc.ColumnSpan = 1;
                       dgi.Cells.Add(tc);
                       tc1.Text = "ddd2";
                       tc1.ColumnSpan = 1;
                       dgi.Cells.Add(tc1);
                       tc2.Text = "ddd";
                       tc2.ColumnSpan = 1;
                       dgi1.Cells.Add(tc2);
                       tc22.Text = "ddd2";
                       tc22.ColumnSpan = 1;
                       dgi1.Cells.Add(tc22);
                 
                 }


VB.NET显示双层表头
    Dim dgi As DataGridItem
        Dim dgi1 As DataGridItem
        Dim tc As New TableCell()
        Dim tc1 As New TableCell()
        Dim tc2 As New TableCell()
        Dim tc22 As New TableCell()
        Dim tb As New Table()
        If (e.Item.ItemType = ListItemType.Header) Then

            dgi = New DataGridItem(0, -1, ListItemType.Header)
            dgi1 = New DataGridItem(0, -1, ListItemType.Header)
            tb = CType(DataGrid1.Controls(0), Table)
            tb.Rows.AddAt(0, dgi)
            tb.Rows.AddAt(1, dgi1)
            tc.Text = "ddd"
            tc.ColumnSpan = 1
            dgi.Cells.Add(tc)
            tc1.Text = "ddd2"
            tc1.ColumnSpan = 1
            dgi.Cells.Add(tc1)

            tc2.Text = "ddd"
            tc2.ColumnSpan = 1
            dgi1.Cells.Add(tc2)
            tc22.Text = "ddd2"
            tc22.ColumnSpan = 1
            dgi1.Cells.Add(tc22)
        End If
posted @ 2005-08-22 13:36  PointNet  阅读(2099)  评论(0编辑  收藏  举报