将多个页脚行添加到Gridview

时间:2012-10-16 19:44:20

标签: asp.net vb.net

我正在尝试向页脚添加更多行,但我的代码一直出错。我已经在这方面工作了几天。请帮忙!

Partial Class Capacity_Plan_Default
    Inherits System.Web.UI.Page

    Private Property EngTotalQuoteHours As Decimal
    Private Property EngTotalUsedHours As Decimal
    Private Property column1 As Decimal
    Private Property column2 As Decimal
    Private Property column3 As Decimal
    Private Property CNC_5Axis As Decimal
    Private Property CNC_Large As Decimal
    Private Property CNC_Small As Decimal
    Private Property EDM_Large As Decimal
    Private Property EDM_Small As Decimal
    Private Property EDM_Trodes As Decimal
    Private Property Assy As Decimal
    Private Property Polish As Decimal
    Private Property Sample As Decimal

    Private Property orginalDatatable As Data.DataTable

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


        If e.Row.RowType = DataControlRowType.DataRow Then

            Dim hourRow As GridViewRow = e.Row

            If Not IsDBNull(DataBinder.Eval(e.Row.DataItem, "Eng_Quoted_Hrs")) Then
                EngTotalQuoteHours += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Eng_Quoted_Hrs"))
            End If
            If Not IsDBNull(DataBinder.Eval(e.Row.DataItem, "Eng_Used_Hrs")) Then
                EngTotalUsedHours += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Eng_Used_Hrs"))
            End If
            If Not IsDBNull(DataBinder.Eval(e.Row.DataItem, "column1")) Then
                column1 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "column1"))
            End If
            If Not IsDBNull(DataBinder.Eval(e.Row.DataItem, "column2")) Then
                column2 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "column2"))
            End If
            If Not IsDBNull(DataBinder.Eval(e.Row.DataItem, "column3")) Then
                column3 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "column3"))
            End If
            If Not IsDBNull(DataBinder.Eval(e.Row.DataItem, "CNC_5Axis")) Then
                CNC_5Axis += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "CNC_5Axis"))
            End If
            If Not IsDBNull(DataBinder.Eval(e.Row.DataItem, "CNC_Large")) Then
                CNC_Large += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "CNC_Large"))
            End If
            If Not IsDBNull(DataBinder.Eval(e.Row.DataItem, "CNC_Small")) Then
                CNC_Small += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "CNC_Small"))
            End If
            If Not IsDBNull(DataBinder.Eval(e.Row.DataItem, "EDM_Large")) Then
                EDM_Large += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "EDM_Large"))
            End If
            If Not IsDBNull(DataBinder.Eval(e.Row.DataItem, "EDM_Small")) Then
                EDM_Small += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "EDM_Small"))
            End If
            If Not IsDBNull(DataBinder.Eval(e.Row.DataItem, "EDM_Trodes")) Then
                EDM_Trodes += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "EDM_Trodes"))
            End If
            If Not IsDBNull(DataBinder.Eval(e.Row.DataItem, "Assy")) Then
                Assy += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Assy"))
            End If
            If Not IsDBNull(DataBinder.Eval(e.Row.DataItem, "Polish")) Then
                Polish += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Polish"))
            End If
            If Not IsDBNull(DataBinder.Eval(e.Row.DataItem, "Sample")) Then
                Sample += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Sample"))
            End If

        ElseIf e.Row.RowType = DataControlRowType.Footer Then
            e.Row.Cells(0).Text = "Total Hours:"
            e.Row.Cells(5).Text = EngTotalQuoteHours.ToString
            e.Row.Cells(6).Text = EngTotalUsedHours.ToString
            e.Row.Cells(7).Text = column1.ToString
            e.Row.Cells(8).Text = column2.ToString
            e.Row.Cells(9).Text = column3.ToString
            e.Row.Cells(10).Text = CNC_5Axis.ToString
            e.Row.Cells(11).Text = CNC_Large.ToString
            e.Row.Cells(12).Text = CNC_Small.ToString
            e.Row.Cells(13).Text = EDM_Large.ToString
            e.Row.Cells(14).Text = EDM_Small.ToString
            e.Row.Cells(15).Text = EDM_Trodes.ToString
            e.Row.Cells(16).Text = Assy.ToString
            e.Row.Cells(17).Text = Polish.ToString
            e.Row.Cells(18).Text = Sample.ToString

            Dim GridView1 As GridView = CType(sender, GridView)
            ''gets the current footer row to clone
            Dim footer As GridViewRow = GridView1.FooterRow
            Dim numCells = footer.Cells.Count

            Dim newRow As New GridViewRow(footer.RowIndex + 1, -1, footer.RowType, footer.RowState)

            ''have to add in the right number of cells
            ''this also copies any styles over from the original footer
            For i As Integer = 0 To numCells - 1
                Dim emptyCell As New TableCell
                emptyCell.ApplyStyle(GridView1.Columns(i).ItemStyle)

                newRow.Cells.Add(emptyCell)
            Next

            newRow.Cells(5).Text = (EngTotalQuoteHours / 46).ToString

            ''add new row to the gridview table, at the very bottom
            CType(GridView1.Controls(0), Table).Rows.Add(newRow)

        End If
    End Sub
End Class

我得到的错误如下。

对象引用未设置为对象的实例。 描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪,以获取有关错误及其在代码中的起源位置的更多信息。

异常详细信息:System.NullReferenceException:未将对象引用设置为对象的实例。

来源错误:

第90行:''获取要克隆的当前页脚行 第91行:Dim footer As GridViewRow = GridView1.FooterRow 第92行:Dim numCells = footer.Cells.Count 第93行: 第94行:Dim newRow As New GridViewRow(footer.RowIndex + 1,-1,footer.RowType,footer.RowState)

1 个答案:

答案 0 :(得分:0)

我认为在这种情况下GridView1.FooterRow为null而不是

Dim footer As GridViewRow = GridView1.FooterRow 

使用

Dim footer As GridViewRow = e.Row 
相关问题