0

我正在使用 VS2010 和 VB.net 2.0。我已经修改了我在网上找到的代码,可以让我批量更新网格视图。gridview 的 datakey 是一个运营商编号(由变量 currentID 引用),大部分是 3 位,少数是 4 位。该代码运行良好,但涉及到具有 4 位操作员编号的行除外。4 位操作符编号返回错误 Index was outside the bounds of the array 当它到达此语句时:

Dim gvMgr_row As System.Data.DataRow = gvMgr_originalDataTable.Select(String.Format("op_num = {0}", currentID))(0)

我一直无法确定为什么 4 位数字会导致问题,并希望得到任何帮助或建议。所有相关代码都应包含在下面。

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

    ' Rebind the Grid to repopulate the original values table.
    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
4

0 回答 0