asp.net gridview 多行标题合并

效果图

 

代码

    Private Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
        '代码换行
        If e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.Cells(e.Row.Cells.Count - 1).Attributes.Add("style", "word-break :break-all ; word-wrap:break-word")
        End If
                
        'gridview多行标题行
        '假设前6列2行合并,后每3列合并一次
        If e.Row.RowType = DataControlRowType.Header Then
            '建立新行
            Dim rowHeader As GridViewRow = New GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal)
            
            '设置颜色及字体
            rowHeader.BackColor = System.Drawing.Color.White
            rowHeader.Font.Bold = True
            
            '获得Gridview行对象
            Dim cells As TableCellCollection = e.Row.Cells
            '隐藏前6列 这是关键,不然前6列多出
            Dim headerCell As TableCell
            For i As Integer = 0 To 5
                cells(i).Visible = False
                headerCell = New TableCell()
                headerCell.Text = e.Row.Cells(i).Text
                '设置合并行数
                headerCell.RowSpan = 2
                '添加单元格
                rowHeader.Cells.Add(headerCell)
                '设置对齐方式
                rowHeader.Cells(i).VerticalAlign = VerticalAlign.Middle
                rowHeader.Cells(i).HorizontalAlign = HorizontalAlign.Center
            Next
            Dim j As Integer = 0
            For i As Integer = 6 To e.Row.Cells.Count - 1
                If i Mod 3 = 0 Then
                    headerCell = New TableCell()
                    headerCell.Text ="新插入行的每3列显示标题"
                    '设置合并行数
                    headerCell.ColumnSpan = 3
                    '添加单元格
                    rowHeader.Cells.Add(headerCell)
                    '设置对齐方式
                    j = j + 1
                    rowHeader.Cells(5 + j).HorizontalAlign = HorizontalAlign.Center

                End If
            Next
            '显示行
            rowHeader.Visible = True
            '把行加在第一行前面
            GridView1.Controls(0).Controls.AddAt(0, rowHeader)

        End If


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值