2012-05-04 195 views
0

我有一個4列的gridview,其中三個是模板字段,用戶可以輸入所需信息,然後單擊一個按鈕將其提交給數據庫。我有2個問題:gridview循環問題

  1. 當我點擊我的按鈕,在第一行的帖子
    消失添加第二個行...的數據。我希望能夠添加行,而不需要第一行的信息 在多個記錄條目情況下消失。我的代碼如下:

    Private Sub SetInitialRow() 
        Dim dts As New DataTable() 
        Dim drs As DataRow = Nothing 
        dts.Columns.Add(New DataColumn("Approval Date", GetType(String))) 
        dts.Columns.Add(New DataColumn("Total Amount", GetType(String))) 
        dts.Columns.Add(New DataColumn("Comments", GetType(String))) 
        dts.Columns.Add(New DataColumn("Initials", GetType(String))) 
        drs = dts.NewRow() 
        drs("Approval Date") = String.Empty 
        drs("Total Amount") = String.Empty 
        drs("Comments") = String.Empty 
        drs("Initials") = String.Empty 
        dts.Rows.Add(drs) 
        ViewState("CurrentTable") = dts 
        gvOLIAdj.DataSource = dts 
        gvOLIAdj.DataBind() 
    End Sub 
    
    Private Sub AddNewRowToGrid() 
        Dim rowIndex As Integer = 0 
        If ViewState("CurrentTable") IsNot Nothing Then 
         Dim dtCurrentTable As DataTable = DirectCast(ViewState("CurrentTable"), DataTable) 
         Dim drCurrentRow As DataRow = Nothing 
         If dtCurrentTable.Rows.Count > 0 Then 
          For i As Integer = 1 To dtCurrentTable.Rows.Count 
           'extract the TextBox values 
           Dim box1 As TextBox = DirectCast(gvOLIAdj.Rows(rowIndex).Cells(1).FindControl("txtAdjAppr"), TextBox) 
           Dim box2 As TextBox = DirectCast(gvOLIAdj.Rows(rowIndex).Cells(2).FindControl("txtAdjAmt"), TextBox) 
           Dim box3 As TextBox = DirectCast(gvOLIAdj.Rows(rowIndex).Cells(3).FindControl("txtCmmts"), TextBox) 
           drCurrentRow = dtCurrentTable.NewRow() 
           drCurrentRow("Approval Date") = box1.Text 
           dtCurrentTable.Rows(i - 1)("Total Amount") = box2.Text 
           dtCurrentTable.Rows(i - 1)("Comments") = box3.Text 
           'dtCurrentTable.Rows(i - 1)("Initials") = 
           rowIndex += 1 
          Next 
          dtCurrentTable.Rows.Add(drCurrentRow) 
          ViewState("CurrentTable") = dtCurrentTable 
          gvOLIAdj.DataSource = dtCurrentTable 
          gvOLIAdj.DataBind() 
         End If 
        Else 
         Response.Write("ViewState is null") 
        End If 
        'Set Previous Data on Postbacks 
        SetPreviousData() 
    End Sub 
    
    Private Sub SetPreviousData() 
        Dim rowIndex As Integer = 0 
        If ViewState("CurrentTable") IsNot Nothing Then 
         Dim dats As DataTable = DirectCast(ViewState("CurrentTable"), DataTable) 
         If dats.Rows.Count > 0 Then 
          For i As Integer = 0 To dats.Rows.Count - 1 
           Dim box1 As TextBox = DirectCast(gvOLIAdj.Rows(rowIndex).Cells(1).FindControl("txtAdjAppr"), TextBox) 
           Dim box2 As TextBox = DirectCast(gvOLIAdj.Rows(rowIndex).Cells(2).FindControl("txtAdjAmt"), TextBox) 
           Dim box3 As TextBox = DirectCast(gvOLIAdj.Rows(rowIndex).Cells(3).FindControl("txtCmmts"), TextBox) 
           box1.Text = dats.Rows(i)("Approval Date").ToString() 
           box2.Text = dats.Rows(i)("Total Amount").ToString() 
           box3.Text = dats.Rows(i)("Comments").ToString() 
           rowIndex += 1 
          Next 
         End If 
        End If 
    End Sub 
    
    Protected Sub btnAddNewRow_Click(sender As Object, e As EventArgs) Handles btnAddNewRow.Click 
        AddNewRowToGrid() 
    
    End Sub 
    
  2. 當我嘗試寫一個循環,將遍歷每個模板字段的搶 的數據,並把它放到我的數據庫不承認,我有數據 模板字段?這是我迄今無果試圖...

    Protected Sub btn_Update_Click(sender As Object, e As EventArgs) Handles btn_Update.Click 
        For Each row As GridViewRow In gvOLIAdj.Rows 
         For Each gv As GridViewRow In gvOLIAdj.Rows 
          Dim appDt As String = (Rows(rowIndex).Cells(1).FindControl("txtAdjAppr")), TextBox) 
          Dim approvalDt As String = CType(gv.FindControl("txtAdjAppr"), TextBox).Text 
          Dim totalAmt As String = CType(gv.FindControl("txtAdjAmt"), TextBox).Text 
          Dim comments As String = CType(gv.FindControl("txtcmmts"), TextBox).Text 
          Dim intitials As String = DirectCast(gv.FindControl("total"), TextBox).Text 
        Next 
    
    End Sub 
    
+0

請格式化您的問題,以便代碼清晰可辨。 – PaulG

+0

我做什麼,當我提交它的變化我不知道我在做什麼... – developthestars

回答

0

在你AddNewRowToGrid方法,則需要執行以下操作:

  dtCurrentTable.Rows(i - 1)("Total Amount") = box2.Text 
      dtCurrentTable.Rows(i - 1)("Comments") = box3.Text 

什麼是錯在這裏是行的更新in dtCurrentTable.Rows(i - 1)。您正在更新您想要更新的行上面的值。