向网格视图添加额外的标题行,该行跨越数据源定义的许多列

时间:2018-12-03 11:58:02

标签: vb.net

我有gridview从SQL数据源(通过页面上其他位置的下拉列表过滤)接收其数据。它是由SQL Server生成的动态数据透视表,其列数各不相同,例如

enter image description here

我希望添加一个摘要标题来对列标题进行分组,例如阶段1,阶段2,阶段3,诸如此类...

enter image description here

我还有另一个SQL数据源(称为dsTableHeadings),它为我提供了该数据集在每个阶段中的列数。

我需要在代码背后创建代码以插入此附加行。我已经找到了针对固定数量的列执行此操作的方法,如下所示,但是还没有解决如何遍历dsTableHeadings数据源来动态执行此操作的问题。

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

    Dim gridrow As GridViewRow = e.Row
    If gridrow.RowType = DataControlRowType.Header Then
        Dim row As New GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal)
        Dim i As Integer = 0

        Dim cell As New TableCell()
        cell.ColumnSpan = 1
        cell.HorizontalAlign = HorizontalAlign.Center
        cell.Text = ""
        row.Cells.Add(cell)

        cell = New TableCell()
        cell.ColumnSpan = 3
        cell.HorizontalAlign = HorizontalAlign.Center
        cell.Text = "Stage 1"
        row.Cells.Add(cell)

        cell = New TableCell()
        cell.ColumnSpan = 2
        cell.HorizontalAlign = HorizontalAlign.Center
        cell.Text = "Stage 2"
        row.Cells.Add(cell)

        cell = New TableCell()
        cell.ColumnSpan = 2
        cell.HorizontalAlign = HorizontalAlign.Center
        cell.Text = "Stage 3"
        row.Cells.Add(cell)

        GridView1.Controls(0).Controls.AddAt(0, row)

    End If

End Sub

问题部分得到解决:这是我转换数据源所需的CType位。

   For Each RecordRow As DataRow In CType(dsTableHeadings.Select_
      (DataSourceSelectArguments.Empty),DataView).ToTable.Rows

        cell = New TableCell()
        cell.ColumnSpan = RecordRow("Cols")
        cell.HorizontalAlign = HorizontalAlign.Center
        cell.Text = RecordRow("ColTitle")
        row.Cells.Add(cell)

    Next

但是,这会添加td单元而不是th单元-任何想法都可以吗?

0 个答案:

没有答案