2012-10-16 83 views
2

我想向頁腳添加更多的行,但我不斷收到與我的代碼錯誤。我已經爲此工作了幾天。請幫忙!添加多個頁腳行到Gridview

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頁腳作爲GridViewRow = GridView1.FooterRow 92行:Dim numCells = footer.Cells.Count 線93 :94 行:Dim NEWROW作爲新GridViewRow(footer.RowIndex + 1,-1,footer.RowType,footer.RowState)

+1

如果您遇到錯誤,您應該在您的問題中發佈該錯誤。這非常重要。 – Gromer

+0

剛剛添加了我得到的錯誤消息。 – user1751135

回答

0

我認爲在這種情況下GridView1.FooterRow爲空,以便代替

Dim footer As GridViewRow = GridView1.FooterRow 

使用

Dim footer As GridViewRow = e.Row 
+0

這解決了我的問題!非常感謝你!! – user1751135