'How to get the rowindex by searching text from datagridview on vbnet
I am stuck on a simple thing. My app has a datagridview
with 3 columns. One columnshave the name "Sites" and I have added the rows to it as in: sites1, site2, etc.
What I want is for it to give me the simple function code to get the rowindex
by searching the name of the site, like site1, from the "Sites" column.
I am messing with the below function but failed. Does you can please modified below function for me. So by this I will be able to get the rowindex by searching site value like site1, site2 etc.
Function FindValue(ByRef dgv As DataGridView, ByVal metric_key As Object) As DataGridViewRow
For Each row As DataGridViewRow In dgv.Rows
If row.Cells.Item("metric_value").Value = metric_key Then
Return row
End If
Next
Return Nothing
End Function
Usage of function dataGridView1.FindValue(1)
Finally I found the way by my self to find the rowindex by value
Here is mine code
Dim rowindex As String
For Each row As DataGridViewRow In DataGridView1.Rows
If row.Cells.Item("yourcolumnnamehere").Value = "valueforwhichyouaresearching" Then
rowindex = row.Index.ToString()
MsgBox(rowindex)
Solution 1:[1]
Maybe you mean ...
For Each cell In DataGridView1.SelectedCells
If Not FirstValue Then
TextBox1.Text += cell.Value.ToString() & ", "
Else
TextBox1.Text += cell.Value.ToString()
FirstValue= False
End If
Next
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | matzone |