vb.net 如何通过在 vbnet 上从 datagridview 搜索文本来获取行索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17496523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:10:10  来源:igfitidea点击:

How to get the rowindex by searching text from datagridview on vbnet

vb.netdatagridviewrows

提问by wahab wahab1

I am stuck on a simple thing. My app has a datagridviewwith 3 columns. One columnshave the name "Sites" and I have added the rows to it as in: sites1, site2, etc.

我被困在一件简单的事情上。我的应用程序有datagridview3 列。一列的名称为“站点”,我已将行添加到其中:site1、site2 等。

What I want is for it to give me the simple function code to get the rowindexby searching the name of the site, like site1, from the "Sites" column.

我想要的是它rowindex通过从“站点”列中搜索站点的名称(如 site1)来为我提供简单的功能代码。

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.

我搞乱了下面的功能,但失败了。你能不能帮我修改下面的功能。因此,通过此我将能够通过搜索站点值(如站点 1、站点 2 等)来获取行索引。

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)

函数 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)

回答by matzone

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