在列datagridview中搜索返回值对应值datadgridview vb.net

ABA*_*UNT 0 vb.net search datagridview

我有一个datagridviewvb.net列的。第一列是产品描述,第二列是产品编号,第三列是价格。

我想按产品编号搜索datagridview并返回价格列中的相应值。

我可以在 s 中搜索文本datagridview,但无法读取相应单元格的值,例如价格单元格。

Private Sub Button33_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button33.Click
    Dim rowindex As String
    For Each row As DataGridViewRow In DataGridView1.Rows
        If row.Cells.Item("ITEM_ID").Value = TextBox5.Text Then
            rowindex = row.Index.ToString()

            Dim actie As String = row.Cells("PRICE").Value.ToString()
            MsgBox(actie)

        Else

            MsgBox("Item not found")

        End If
    Next
End Sub
Run Code Online (Sandbox Code Playgroud)

Woz*_*zeC 5

好的,感谢您更新代码。做这个:

Private Sub Button33_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button33.Click
    Dim rowindex As String
    Dim found as Boolean = false
    For Each row As DataGridViewRow In DataGridView1.Rows
        If row.Cells.Item("ITEM_ID").Value = TextBox5.Text Then
            rowindex = row.Index.ToString()
            found = true
            Dim actie As String = row.Cells("PRICE").Value.ToString()
            MsgBox(actie)
            Exit for
        End If
    Next
    If Not found Then
       MsgBox("Item not found")
    End if    
End Sub
Run Code Online (Sandbox Code Playgroud)

它的作用是循环遍历所有项目。当它找到匹配项时,会将找到的值设置为 true。如果未找到项目,则循环结束时“found”为 false。如果“找到”为假,则显示“未找到项目”。希望你能理解,否则再问:)