我在Datagridview中的值是
| warranty |
---------------
| 0001 |
| 0002 |
| 0003 |
| 0004 |
代码值:
Private Sub UpdateGrid()
Dim str As String = "select Warranty from TBwarranty"
ds = New DataSet
da = New OleDbDataAdapter(str, connection)
da.Fill(ds, "a")
DataGridView1.DataSource = ds.Tables("a")
End Sub
请有人真的能告诉我我有此错误的代码有什么问题吗
System.ArgumentOutOfRangeException:'索引超出范围。必须为非负数并且小于集合的大小。参数名称:index'
起初我编码:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim c As Integer = DataGridView1.SelectedRows.Count() - 1
For i As Integer = 0 To c
Dim deleteQuery As String = "DELETE from TBwarranty WHERE warranty='" & DataGridView1.SelectedRows(i).Cells(0).Value & "'"
Try
T.ExecuteQuery(deleteQuery, connection) 'This my public sub in other class [ command.ExecuteNonQuery() ]
UpdateGrid()
Catch ex As System.Data.OleDb.OleDbException
MsgBox(ex.Message)
MessageBox.Show("Fail inserted.")
End Try
Next
End Sub
然后上面有一个问题错误。
然后我尝试编码:
Dim c As Integer = DataGridView1.SelectedRows.Count() - 1
For i As Integer = 0 To c
Dim deleteQuery As String = "DELETE from TBwarranty WHERE warranty='" & DataGridView1.SelectedRows(i).Cells(0).Value & "'"
Console.WriteLine(deleteQuery)
Next
没有任何错误,输出到控制台是真实值。
我不明白为什么会这样?怎么了以及我该怎么办?感谢您的协助。
答案 0 :(得分:0)
根据评论的意见。现在我可以解决错误了。
Dim c As Integer = DataGridView1.SelectedRows.Count() - 1
For i As Integer = 0 To c
Dim deleteQuery As String = "DELETE from TBwarranty WHERE warranty='" & DataGridView1.SelectedRows(i).Cells(0).Value & "'"
Console.WriteLine(deleteQuery)
Try
T.ExecuteQuery(deleteQuery, connection)
Catch ex As System.Data.OleDb.OleDbException
MsgBox(ex.Message)
MessageBox.Show("Failed inserted.")
End Try
Next
UpdateGrid()