搜索 Datagridview Vb.net
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25411844/
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
Search Datagridview Vb.net
提问by Naeem Sardar Bhatti
I have been stuck in creating search for Datagridview in Vb.net.
我一直坚持在 Vb.net 中创建对 Datagridview 的搜索。
I have one DataGridViewthat is bound to binding source. It contains data such as:
我有一个DataGridView绑定到绑定源。它包含以下数据:
123456,
213926,
285643,
395687,
I have searched but everywhere but I only found a filter method for binding source or a find method. The filter method removes remaining rows & find method finds an exact string.
我已经搜索过但到处都是,但我只找到了一个用于绑定源的过滤器方法或一个查找方法。filter 方法删除剩余的行 & find 方法找到一个确切的字符串。
I found a method to find text in DataGridViewbut that search found the string anywhere in DataGridViewcolumn like if user type 2 then it will first select the row having 2 such as 123456.
我找到了一种在其中查找文本的方法,DataGridView但是该搜索在DataGridView列中的任何位置找到了字符串,就像用户键入 2 一样,它会首先选择具有 2 的行,例如 123456。
I want to create search that should find letter in sequence from start & so on. If the user presses 2 then search should go for cell starting with 2 such as 213926.
我想创建应该从开始等顺序查找字母的搜索。如果用户按 2,则搜索应以 2 开头的单元格,例如 213926。
采纳答案by Naeem Sardar Bhatti
Matter solved Frank. Thankyou So Much. Here is my complete code for others too.
事情解决了弗兰克。太感谢了。这也是我给其他人的完整代码。
Dim Found As Boolean = False
Dim StringToSearch As String = ""
Dim ValueToSearchFor As String = Me.TextBox1.Text.Trim.ToLower
Dim CurrentRowIndex As Integer = 0
Try
If dgvDishonourReceipts.Rows.Count = 0 Then
CurrentRowIndex = 0
Else
CurrentRowIndex = dgvDishonourReceipts.CurrentRow.Index + 1
End If
If CurrentRowIndex > dgvDishonourReceipts.Rows.Count Then
CurrentRowIndex = dgvDishonourReceipts.Rows.Count - 1
End If
If dgvDishonourReceipts.Rows.Count > 0 Then
For Each gRow As DataGridViewRow In dgvDishonourReceipts.Rows
StringToSearch = gRow.Cells(4).Value.ToString.Trim.ToLower
If InStr(1, StringToSearch, LCase(Trim(TextBox1.Text)), vbTextCompare) = 1 Then
Dim myCurrentCell As DataGridViewCell = gRow.Cells(4)
Dim myCurrentPosition As DataGridViewCell = gRow.Cells(0)
dgvDishonourReceipts.CurrentCell = myCurrentCell
CurrentRowIndex = dgvDishonourReceipts.CurrentRow.Index
Found = True
End If
If Found Then Exit For
Next
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try

