效果图
代码
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