解决方案索引超出范围。必须是非负的,并且小于vb.net中收藏的大小
我有一个错误问题“索引超出了范围。 DataGridView中的“代码”列。有最好的建议吗?
Public x As Integer
Dim source1 As New BindingSource()
Private Sub TextBox2_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles TextBox2.KeyDown
If e.KeyCode = Keys.Back Then
source1.Filter = ""
TextBox1.Clear()
Else
If e.KeyCode = Keys.Enter Then
source1.Filter = "CODE = '" & Replace(TextBox2.Text, "'", "") & "'"
x = DataGridView1.Rows.IndexOf(DataGridView1.CurrentRow)
txtCODE.Text = DataGridView1.Rows(x).Cells(0).Value.ToString()
Dim dte = DataGridView1.Rows(x).Cells(1).Value.ToString()
If dte <> Nothing Then
txtDTE.Text = CDate(dte)
Else
'CLEAR txtDTE if dt <> nothing
txtDTE.Clear()
End If
txtQTY.Text = DataGridView1.Rows(x).Cells(2).Value.ToString()
txtPRICE.Text = DataGridView1.Rows(x).Cells(3).Value.ToString()
txtPRICE.Text = String.Format(System.Globalization.CultureInfo.GetCultureInfo("en-US"), "{0:N0}", Double.Parse(txtPRICE.Text))
If source1.Count <= 0 Then
source1.Filter = ""
TextBox2.Clear()
MsgBox("No Result Found!", MsgBoxStyle.Exclamation)
End If
End If
End If
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过添加检查来修复它,以确保当前行的索引有效。
I fixed it by adding a check to make sure that the index of the current row is valid.