このボタンイベントを実行するとエラーが発生します: これが私のコードです:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Try
' get the details of the item
Dim R As Your_pharmacy.POSDS.ItemsRow = Button1.Tag
' next search for the barcode in the datagridview
Dim I As Integer
Dim ItemLoc As Integer = -1
For I = 0 To DGV1.Rows.Count - 1
If R.barcodeNumber = DGV1.Rows(I).Cells(0).Value Then
' item found
ItemLoc = I
Exit For
End If
Next
' if item is not found, add it
If ItemLoc = -1 Then
DGV1.Rows.Add(R.barcodeNumber, R.ItemName, R.BuyPrice, R.SellPrice, 1, R.SellPrice)
Else
' if item is already there increase its count
Dim ItemCount As Long = DGV1.Rows(ItemLoc).Cells(4).Value
ItemCount += 1
Dim NewPrice As Decimal = R.SellPrice * ItemCount
DGV1.Rows(ItemLoc).Cells(4).Value = ItemCount
DGV1.Rows(ItemLoc).Cells(5).Value = NewPrice
End If
' next clear textbox1 and set focus to it
TextBox1.Text = ""
TextBox1.Focus()
' compute the total for the recipt
Dim Sum As Decimal = 1
For I = 0 To DGV1.Rows.Count - 1
Sum += DGV1.Rows(I).Cells(5).Value 'here the error happens
Next
TextBox4.Text = Sum
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical Or MsgBoxStyle.OkOnly, "Error")
End Try
End Sub
エラーの詳細:
エラー: インデックスが範囲外でした。負ではなく、コレクションのサイズ未満である必要があります。パラメータ名: インデックス vb.net