我在搜索datagridview中的项目时遇到问题 这是我的代码,但每当我搜索数据库中已经存在的项目时,它就是告诉未找到
If txtfirstname.Text = "" Then
MsgBox("Please enter first name!")
Else
Dim totalrow As Integer = DataGridView1.RowCount - 2
Dim rowin As Integer
Dim flag As Boolean = False
Dim sear As String = CStr(txtfirstname.Text)
For rowin = 0 To totalrow
Dim id As String = DataGridView1.Item(0, rowin).Value
If sear = id Then
DataGridView1.ClearSelection()
DataGridView1.Rows(rowin).Selected = True
DataGridView1.CurrentCell = DataGridView1.Item(0, rowin)
flag = True
Exit Sub
Else
flag = False
End If
Next rowin
If flag = False Then
MessageBox.Show("Firstname " & txtfirstname.Text & " is not found in database.", "Search Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End If
答案 0 :(得分:0)
通过设置
Dim totalrow As Integer = DataGridView1.RowCount - 2
您始终缺少数据集中的最后一条记录。
尝试
Dim totalrow As Integer = DataGridView1.RowCount - 1
设置For
循环的上限值。