0

我在 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
4

1 回答 1

0

通过设置

Dim totalrow As Integer = DataGridView1.RowCount - 2

您总是缺少数据集中的最后一条记录。

尝试

Dim totalrow As Integer = DataGridView1.RowCount - 1

For设置循环的上限值。

于 2012-05-10T10:40:17.340 回答