添加自定义标题行后添加thead到GridView?

时间:2013-08-16 17:43:02

标签: asp.net vb.net gridview

我在Google和Stack Overflow上搜索过,但我似乎无法找到答案。

我只想在我的GridView中添加<thead>标记,以便我可以使用jQuery表格分类器。

我像这样添加<thead>部分:

Protected Sub GvReportPreRender(ByVal sender As Object, ByVal e As EventArgs) Handles gvReport.PreRender
     If gvReport.Rows.Count > 0 Then
          gvReport.UseAccessibleHeader = True
          gvReport.HeaderRow.TableSection = TableRowSection.TableHeader
     End If
End Sub

但是,我发现它失败了,因为我使用以下方法在RowDataBound上添加自定义行和样式。有没有什么方法可以在保留<thead>标记的同时在正常标题之上添加此自定义标题行,而不会破坏我计划用jQuery实现的任何排序功能?

Private Sub StyleTable(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles gvReport.RowDataBound
            Dim useDollars As Boolean = False

            'Define arrays to color the gridview, if cell index is in array, it will be colored
            Dim blueArray() As Integer = {0, 17, 18, 19, 20}
            Dim greenArray() As Integer = {1, 2, 3, 4}
            Dim purpleArray() As Integer = {5, 6, 7, 8}
            Dim pinkArray() As Integer = {9, 10, 11, 12}
            Dim yellowArray() As Integer = {13, 14, 15, 16}

            If filterControl.SelectedMeasurement = "NetSales" Then
                useDollars = True
            End If

            _packworks.ColorizeMe(blueArray, greenArray, purpleArray, pinkArray, yellowArray, e.Row)

            If e.Row.RowType = DataControlRowType.Header Then
                Dim headerGridRow As New GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert)
                Dim headerCell As New TableCell()

                'Reset color arrays for headers
                blueArray = New Integer() {0, 5}
                greenArray = New Integer() {1}
                purpleArray = New Integer() {2}
                pinkArray = New Integer() {3}
                yellowArray = New Integer() {4}

                Dim lblForecastTotal As Label = CType(e.Row.FindControl("lblHForecast_total"), Label)
                Dim lblActualTotal As Label = CType(e.Row.FindControl("lblHActual_total"), Label)
                Dim lblForecastQ1 As Label = CType(e.Row.FindControl("lblHForecast_q1"), Label)
                Dim lblActualQ1 As Label = CType(e.Row.FindControl("lblHActual_q1"), Label)
                Dim lblForecastQ2 As Label = CType(e.Row.FindControl("lblHForecast_q2"), Label)
                Dim lblActualQ2 As Label = CType(e.Row.FindControl("lblHActual_q2"), Label)
                Dim lblForecastQ3 As Label = CType(e.Row.FindControl("lblHForecast_q3"), Label)
                Dim lblActualQ3 As Label = CType(e.Row.FindControl("lblHActual_q3"), Label)
                Dim lblForecastQ4 As Label = CType(e.Row.FindControl("lblHForecast_q4"), Label)
                Dim lblActualQ4 As Label = CType(e.Row.FindControl("lblHActual_q4"), Label)

                'Business wants to compare Year - 1 forecast data to Year actuals, so set labels
                'Accordingly.. Actual logic is applied when calling GetForecastTable() and
                'GetDnsTable()
                Dim lblArray() As Label = {lblActualTotal, lblActualQ1, lblActualQ2, _
                                           lblActualQ3, lblActualQ4}

                _packworks.SetHeaderText(filterControl.Oyear, lblArray, Nothing, gvReport)

                lblArray = New Label() {lblForecastTotal, lblForecastQ1, lblForecastQ2, _
                                        lblForecastQ3, lblForecastQ4}

                _packworks.SetHeaderText(filterControl.Oyear - 1, lblArray, Nothing, gvReport)

                headerCell.Text = "Account"
                headerCell.RowSpan = 2
                headerCell.HorizontalAlign = HorizontalAlign.Center
                e.Row.Cells(0).Visible = False
                headerGridRow.Cells.Add(headerCell)

                headerCell = New TableCell()
                headerCell.HorizontalAlign = HorizontalAlign.Center
                headerCell.Text = "Total Packworks" & IIf(useDollars, " ($)", String.Empty).ToString()
                headerCell.ColumnSpan = 4
                headerGridRow.Cells.Add(headerCell)

                headerCell = New TableCell()
                headerCell.HorizontalAlign = HorizontalAlign.Center
                headerCell.Text = "Total Q1 Packworks" & IIf(useDollars, " ($)", String.Empty).ToString()
                headerCell.ColumnSpan = 4
                headerGridRow.Cells.Add(headerCell)

                headerCell = New TableCell()
                headerCell.HorizontalAlign = HorizontalAlign.Center
                headerCell.Text = "Total Q2 Packworks" & IIf(useDollars, " ($)", String.Empty).ToString()
                headerCell.ColumnSpan = 4
                headerGridRow.Cells.Add(headerCell)

                headerCell = New TableCell()
                headerCell.HorizontalAlign = HorizontalAlign.Center
                headerCell.Text = "Total Q3 Packworks" & IIf(useDollars, " ($)", String.Empty).ToString()
                headerCell.ColumnSpan = 4
                headerGridRow.Cells.Add(headerCell)

                headerCell = New TableCell()
                headerCell.HorizontalAlign = HorizontalAlign.Center
                headerCell.Text = "Total Q4 Packworks" & IIf(useDollars, " ($)", String.Empty).ToString()
                headerCell.ColumnSpan = 4
                headerGridRow.Cells.Add(headerCell)

                _packworks.ColorizeMe(blueArray, greenArray, purpleArray, pinkArray, yellowArray, headerGridRow)
                gvReport.Controls(0).Controls.AddAt(0, headerGridRow)

            ElseIf e.Row.RowType = DataControlRowType.DataRow Then
                Dim lblVarianceTotal As Label = CType(e.Row.FindControl("lblVariance_total"), Label)
                Dim lblVarianceQ1 As Label = CType(e.Row.FindControl("lblVariance_q1"), Label)
                Dim lblVarianceQ2 As Label = CType(e.Row.FindControl("lblVariance_q2"), Label)
                Dim lblVarianceQ3 As Label = CType(e.Row.FindControl("lblVariance_q3"), Label)
                Dim lblVarianceQ4 As Label = CType(e.Row.FindControl("lblVariance_q4"), Label)
                Dim lblVarianceTotalPercent As Label = CType(e.Row.FindControl("lblVariance_total_percent"), Label)
                Dim lblVarianceQ1Percent As Label = CType(e.Row.FindControl("lblVariance_q1_percent"), Label)
                Dim lblVarianceQ2Percent As Label = CType(e.Row.FindControl("lblVariance_q2_percent"), Label)
                Dim lblVarianceQ3Percent As Label = CType(e.Row.FindControl("lblVariance_q3_percent"), Label)
                Dim lblVarianceQ4Percent As Label = CType(e.Row.FindControl("lblVariance_q4_percent"), Label)

                Dim lblArray() As Label = {lblVarianceTotal, lblVarianceQ1, lblVarianceQ2, lblVarianceQ3, _
                                            lblVarianceQ4, lblVarianceTotalPercent, lblVarianceQ1Percent, _
                                            lblVarianceQ2Percent, lblVarianceQ3Percent, lblVarianceQ4Percent}

                For Each lbl As Label In lblArray
                    _packworks.MakeNegativesRed(lbl)
                Next
            End If
        End Sub

2 个答案:

答案 0 :(得分:2)

请使用:

Protected Sub GridView1_PreRender(sender As Object, e As System.EventArgs) Handles GridView1.PreRender
    GridView1.UseAccessibleHeader = True
    GridView1.HeaderRow.TableSection = TableRowSection.TableHeader
End Sub

或使用Javascript:

window.onload = function () {
    var grid = document.getElementById('<%= GridView1.ClientID %>');
    var tbody = grid.getElementsByTagName("tbody")[0]; //gets the first and only tbody
    var firstTr = tbody.getElementsByTagName("tr")[0]; //gets the first tr, hopefully contains the th's

    tbody.removeChild(firstTr); //remove tr's from table

    var newTh = document.createElement('thead'); //creates thead
    newTh.appendChild(firstTr); //puts ths in thead
    grid.insertBefore(newTh, tbody); //puts thead behore tbody

}

enter image description here

答案 1 :(得分:0)

GridView1.UseAccessibleHeader = True

只需从上面的代码中删除此行。

我在Page_Load事件中使用过它。

相关问题