0

我有一个三列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
4

2 回答 2

4

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

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

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

于 2013-10-07T13:08:30.823 回答
0

这个对我有用。尝试这个:

If TextBoxSearch.Text IsNot Nothing Then
    Dim IsItemFound As Boolean = False
    Dim ItemPrice As String
    DataGridView1.ClearSelection()
    For Each row As DataGridViewRow In DataGridView1.Rows
        If row.Cells("ItemId").Value.ToString = TextBoxSearch.Text Then
            IsItemFound = True
            ItemPrice = row.Cells("ItemPrice").Value.ToString()
            row.Selected = True
            DataGridViewDetails.FirstDisplayedScrollingRowIndex = row.Index
            Exit For
        End If

        If Not IsItemFound Then
            MsgBox("Item not found")
        End If
    Next
End If
于 2021-12-12T11:52:15.620 回答