vba 搜索 DataGridView 并选择匹配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21499728/
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-12 01:41:08  来源:igfitidea点击:

Searching DataGridView and Selecting Match

vb.netvba

提问by user3224987

How would I search a DataGridView's first column for a Value and select the row IF the Value is there? -- Much like a WHERE Clause in SQL.

我将如何在 DataGridView 的第一列中搜索值并选​​择值所在的行?-- 很像 SQL 中的 WHERE 子句。

I've been looking around for an answer in threads but I can't seem to find one.

我一直在寻找线程中的答案,但似乎找不到。

DataGridView1is the DataGridView and My.Settings.CurrentUserIDis the Value I want to search for.

DataGridView1是 DataGridView 并且My.Settings.CurrentUserID是我要搜索的值。

Any help would be appreciated.

任何帮助,将不胜感激。

Note; There can only be one occurrence of the Value in the table, so there is no concern for multiple returns/matches.

笔记; 表中只能出现一次 Value,因此无需担心多次返回/匹配。

{Edit 1}

{编辑 1}

Steves solution seems to only highlight the row and not actually select it as the Black arrow doesn't move onto it. The value the code's looking for is 4, notice how it finds it and highlights it, but the arrow doesn't move.

Steves 解决方案似乎只突出显示该行而不实际选择它,因为黑色箭头不会移动到它上面。代码正在寻找的值是4,注意它是如何找到它并突出显示它的,但箭头不动。

The code does this:

代码是这样做的:

enter image description here

在此处输入图片说明

I need it to do this:

我需要它来做到这一点:

enter image description here

在此处输入图片说明

采纳答案by Steve

Well supposing that the column you want to search is the first one then

那么假设您要搜索的列是第一个然后

For Each row in DataGridView1.Rows
    If Convert.ToInt32(row.Cells(0).Value) = My.Settings.CurrentUserID Then
        row.Selected = True
        Exit For
    End If
Next

EDIT:

编辑:

To indicate the "current" row, set the CurrentCellproperty of the datagridview.

要指示“当前”行,请设置datagridview的CurrentCell属性。

   .....
   DataGridView1.CurrentCell = row.Cells(0)
   Exit For

From MSDN:

来自 MSDN:

When you set a cell as the current cell, it will scroll into view if it is not currently displayed. The current cell cannot be a header cell, a disabled cell, or a cell in a hidden row or column.

当您将一个单元格设置为当前单元格时,如果它当前未显示,它将滚动到视图中。当前单元格不能是标题单元格、禁用单元格或隐藏行或列中的单元格。

回答by ???ěxě?

Put this in a click event or whatever...

把它放在一个点击事件或其他什么......

    Dim intIndex As Integer = FindUser(txtSearchString.Text, txtColumn.Text)
    dgvMembers.CurrentCell = dgvMembers.Rows(intIndex).Cells(0) 'This ensures that the    arrow will move

Put this in your class or wherever...

把它放在你的课堂上或任何地方......

  Private Function FindUser(ByVal strUserID As String, ByVal intColumn As Integer) As Integer
    Dim intIndex As Integer = 0

    For i As Integer = 0 To dgvMembers.Rows.Count - 1
        If dgvMembers.Rows(i).Cells(intColumn).Value.ToString = strUserID Then
            intIndex = i
            Exit For
        End If
    Next

    Return intIndex
End Function