Search in column datagridview return value coresponding value datadgridview vb.net
36,441
Solution 1
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 :)
Solution 2
It works for me. Try this:
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
Author by
LabRat
Updated on December 13, 2021Comments
-
LabRat over 2 years
I have a
datagridview
invb.net
with three columns. In the first column, a product description, in the second column there is a product number and in the third column, a price is given.I would like to search in a
datagridview
by product number and return the corresponding value in the prices column.I can to search for text in
datagridview
s, but I can't read the value of a corresponding cell such as the price cell.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
-
WozzeC over 10 yearsThis shouldn't be to difficult to solve, I just want to know how the datagridview gets the values and how you search through it.
-
LabRat over 10 yearssee edit i did manage the following but my else keeps repeating for the amount of columns searched in..which if the item is not found would be quite anoying after 100+ columns
-
-
Nico Haase over 2 yearsPlease add some explanation to your answer such that others can learn from it