vb.net 从 TextBox 在 Datagridview 中搜索完整或部分匹配项,并在显示完整数据网格时选择第一个匹配项

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

vb.net Search for Full or partial match in Datagridview from TextBox and select the first match while displaying the full datagrid

vb.netsearchdatagridviewtextboxmatch

提问by wlhj

I have a customers table that is displayed in a datagridview. I would like the user to be able to enter the customers full or partial last name and click a buttom that would then find the first customer that met the match in the text box. As an example: The user types "wil" into the text box and the first record found is for "williams" even though the user is looking for "wilson". The record would be highlighted(selected) and the user could scroll to look at other records and select "wilson" manually (the manual part I can code).

我有一个显示在 datagridview 中的客户表。我希望用户能够输入客户的完整或部分姓氏,然后单击一个按钮,然后在文本框中找到第一个遇到匹配项的客户。例如:用户在文本框中键入“wil”,找到的第一条记录是“williams”,即使用户正在寻找“wilson”。该记录将被突出显示(选中),用户可以滚动查看其他记录并手动选择“wilson”(我可以编码的手动部分)。

I have searched for hours on the internet and cannot find this type of code. Most of it is filtering or searching every cell and returning every value.

我在互联网上搜索了几个小时,找不到这种类型的代码。其中大部分是过滤或搜索每个单元格并返回每个值。

I am currently reworking a project I did with an access database and vba several years ago. I had thought vb.net would be very similar but it is not similar enough for me to modify this code. I'm also going to use a sql database.

我目前正在修改我几年前使用访问数据库和 vba 所做的项目。我原以为 vb.net 会非常相似,但它不够相似,我无法修改此代码。我还将使用 sql 数据库。

The index field is obviously cell(0) and last name is cell(1).

索引字段显然是单元格(0),姓氏是单元格(1)。

I have found a solution although I did have to modify it. It will do everything I need except one thing. If I type the letter "H" and do a search on last name, it finds the first lastname that has an "H" in the last name but in a different position from the first letter. I need it to go to the first last name that begins with an "H". I have listed my code below.

我找到了一个解决方案,尽管我确实必须修改它。除了一件事,它会做我需要的一切。如果我输入字母“H”并搜索姓氏,它会找到姓氏中带有“H”但位置与第一个字母不同的第一个姓氏。我需要它转到以“H”开头的第一个姓氏。我在下面列出了我的代码。

Dim srch As String Dim irowindex As Integer Dim strl As Integer srch = txtSearch.Text

Dim srch As String Dim irowindex As Integer Dim strl As Integer srch = txtSearch.Text

    dgvCustomers.ClearSelection()
    For i As Integer = 0 To dgvCustomers.Rows.Count - 1
        If dgvCustomers.Rows(i).Cells(0).Value IsNot Nothing Then
            If dgvCustomers.Rows(i).Cells(1).Value.ToString.ToUpper.Contains(srch.ToUpper) Then
                dgvCustomers.Rows(i).Selected = True
                dgvCustomers.RowsDefaultCellStyle.SelectionBackColor = Color.DimGray
                irowindex = dgvCustomers.SelectedCells.Item(0).Value
                MessageBox.Show(irowindex)
                Exit For
            End If
        End If
    Next
End Sub

回答by Muj

try this sir.

试试这个先生。

for each row as datagridviewrow in nameofdatagrid.rows
 if row.cells("Lastname").value = txtbox.text then
   nameofdatagrid.clearselection()
   row.cells("Lastname").selected = true
   exit for
 end if
next

I think this is your problem? finding lastname match in the datagrid and select it? hope this will help you :)

我想这是你的问题?在数据网格中找到姓氏匹配并选择它?希望能帮到你 :)

回答by wlhj

Dim srch As String Dim irowindex As Integer Dim strl As Integer srch = txtSearch.Text

Dim srch As String Dim irowindex As Integer Dim strl As Integer srch = txtSearch.Text

    dgvCustomers.ClearSelection()
    For i As Integer = 0 To dgvCustomers.Rows.Count - 1
        If dgvCustomers.Rows(i).Cells(0).Value IsNot Nothing Then
            If dgvCustomers.Rows(i).Cells(1).Value.ToString.ToUpper.StartsWith(srch.ToUpper) Then
                dgvCustomers.Rows(i).Selected = True
                dgvCustomers.RowsDefaultCellStyle.SelectionBackColor = Color.DimGray
                irowindex = dgvCustomers.SelectedCells.Item(0).Value
                MessageBox.Show(irowindex)
                Exit For
            End If
        End If
    Next