I am using VS2010 and VB.net 2.0. I have modified code that I found online that will allow me to mass update a gridview. The datakey for the gridview is an operator number (referenced by variable currentID), most of which are 3 digits, but a few are 4 digits. The code works great except for when it comes to the rows with the 4 digit operator numbers. The 4 digit operator number returns an error of Index was outside the bounds of the array when it gets to this statement:
Dim gvMgr_row As System.Data.DataRow = gvMgr_originalDataTable.Select(String.Format("op_num = {0}", currentID))(0)
I have been unable to determine why 4 digits cause an issue and would appreciate any help or advice. All of the relevant code should be included below.
Protected Sub MgrUpdateButton_Click(sender As Object, e As System.EventArgs) Handles MgrUpdateButton.Click
gvMgr_originalDataTable = CType(ViewState("gvMgr_originalValuesDataTable"), System.Data.DataTable)
For Each r As GridViewRow In gvMgrData.Rows
If gvMgr_IsRowModified(r) Then gvMgrData.UpdateRow(r.RowIndex, False)
Next
gvMgr_tableCopied = False
gvMgrData.DataBind()
End Sub
Protected Function gvMgr_IsRowModified(ByVal r As GridViewRow) As Boolean
Dim currentID As Integer
Dim currentThreshold1 As String
Dim currentPayout1 As String
Dim currentThreshold2 As String
Dim currentPayout2 As String
Dim currentThreshold3 As String
Dim currentPayout3 As String
Dim currentThreshold4 As String
Dim currentPayout4 As String
currentID = Convert.ToInt32(gvMgrData.DataKeys(r.RowIndex).Value)
currentThreshold1 = CType(r.FindControl("Threshold1TextBox"), TextBox).Text
currentPayout1 = CType(r.FindControl("Payout1TextBox"), TextBox).Text
currentThreshold2 = CType(r.FindControl("Threshold2TextBox"), TextBox).Text
currentPayout2 = CType(r.FindControl("Payout2TextBox"), TextBox).Text
currentThreshold3 = CType(r.FindControl("Threshold3TextBox"), TextBox).Text
currentPayout3 = CType(r.FindControl("Payout3TextBox"), TextBox).Text
currentThreshold4 = CType(r.FindControl("Threshold4TextBox"), TextBox).Text
currentPayout4 = CType(r.FindControl("Payout4TextBox"), TextBox).Text
Dim gvMgr_row As System.Data.DataRow = gvMgr_originalDataTable.Select(String.Format("op_num = {0}", currentID))(0)
If Not currentThreshold1.Equals(gvMgr_row("Threshold1").ToString()) Then Return True
If Not currentPayout1.Equals(gvMgr_row("Payout1").ToString()) Then Return True
If Not currentThreshold2.Equals(gvMgr_row("Threshold2").ToString()) Then Return True
If Not currentPayout2.Equals(gvMgr_row("Payout2").ToString()) Then Return True
If Not currentThreshold3.Equals(gvMgr_row("Threshold3").ToString()) Then Return True
If Not currentPayout3.Equals(gvMgr_row("Payout3").ToString()) Then Return True
If Not currentThreshold4.Equals(gvMgr_row("Threshold4").ToString()) Then Return True
If Not currentPayout4.Equals(gvMgr_row("Payout4").ToString()) Then Return True
Return False
End Function