ABANDOND ACOUNT ABANDOND ACOUNT - 1 month ago 13
Vb.net Question

Search in column datagridview return value coresponding value datadgridview vb.net

I have a datagridview in vb.net with three columns

in the first is a product description, in the second is a product number and in the 3rd a price

I would like to search in a datagridview by product number and return the corresponding value in the prices column.

I am able to search for a text in datagridviews but am at current unable to read the value of a corresponding cell such as the price cell.

So to recap, I want to search for a product number but return the products price (to be stored in a variable for further use)

I'm using vb.net

EDDIT:

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

Answer

Allright, thanks for the code update. Do this:

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

What this does is that it loops through all the items. When it finds a match it sets found to true. If not item is found then "found" is false when the loop ends. And if "found" is false then you display "Item not found". Hope you understand, otherwise ask :)