I have a gridview with four template fields that I need to be able to put info into to save to the database. When I hit the edit button and my columns change into edit mode I can enter information just fine… But when I try to loop through the control to get the information it doesn’t pick anything up?
Private Sub gvOLIAdj_RowEditing(sender As Object, e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvOLIAdjst.RowEditing
gvOLIAdjst.EditIndex = e.NewEditIndex
BindData()
End Sub
Here is the update event that I have so far… any ideas? Am I just not doing this right?
Private Sub gvOLIAdj_RowUpdating(sender As Object, e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvOLIAdjst.RowUpdating
Dim dts As DataTable = DirectCast(Session("BudgetsTable"), DataTable)
Dim row As GridViewRow = gvOLIAdjst.Rows(e.RowIndex)
If DirectCast(row.FindControl("txtItemTempTtlAmt"), TextBox).Text = "" Then
lblError.Text = "Invalid Entry. Correct and try again"
e.Cancel = True
Else
lblError.Text = ""
dts.Rows(row.DataItemIndex)("Approval Date") = DirectCast(row.FindControl("txtItemTempApprDt"), TextBox).Text
dts.Rows(row.DataItemIndex)("Total Amount") = DirectCast(row.FindControl("txtItemTempTtlAmt"), TextBox).Text
dts.Rows(row.DataItemIndex)("Comments") = DirectCast(row.FindControl("txtItemTempCmmt"), TextBox).Text
dts.Rows(row.DataItemIndex)("Initials") = DirectCast(row.FindControl("lblItemTempInit"), Label).Text
gvOLIAdjst.EditIndex = -1
BindData()
End If
If Not IsPostBack Then
result = dal.dbConnect(sqlconnection, ConfigurationManager.AppSettings("SQLServerConnection"))
If result = "Successful" Then
dt = ExecuteInsertUpdateDeleteStoredProc(sqlconnection, "@amt", txtItemTempApprDt.text)
dbClose
End If
End If
End Sub
Rather than pulling the controls from the GridView row, I would use the
GridViewUpdateEventArgsvariable that you have access to there. You can pull the values from theNewValuesproperty (there are also other useful properties you might need – likeOldValuesandRowIndex).You access them like this:
Where “Data Field Name” is the key for that value (the name of the field in your
GridViews dataset).Something like this should work
The keys I’m showing up there might not be exactly right, because I can’t see how your binding data to your GridView. You can set a breakpoint and inspect the “NewValues” dictionary to find the exact keys, I believe. Let me know if you have any questions.