[转帖]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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值