从 vb.net 中的数据网格视图中查找文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7976739/
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
Finding text from the datagrid view in vb.net
提问by nightfire001
I have a data grid view in one windows form named "GridViewForm". When the user search for the text from the search box from another window form named "FindForm", I want to highlight all the matching result in the data grid view. The search type can be exact or partial.
我在一个名为“GridViewForm”的窗体中有一个数据网格视图。当用户从另一个名为“FindForm”的窗口表单的搜索框中搜索文本时,我想在数据网格视图中突出显示所有匹配结果。搜索类型可以是精确的或部分的。
For eg.
例如。
If the user search for the text "stack", then the words "stack" from [Stack, stack-over, stacks, stack exchange] should be highlighted and first cell that match the query should be selected. When the user press next button then another cell that match the search query should be selected.
如果用户搜索文本“stack”,则应突出显示 [Stack, stack-over, stacks, stack exchange] 中的单词“stack”,并应选择与查询匹配的第一个单元格。当用户按下下一步按钮时,应选择与搜索查询匹配的另一个单元格。
My code for finding the text is like follow for it search only the exact word.
我用于查找文本的代码就像按照它只搜索确切的单词一样。
Dim gridRow As Integer = 0
Dim gridColumn As Integer = 0
For Each Row As DataGridViewRow In AccountsDataGridView.Rows
For Each column As DataGridViewColumn In AccountsDataGridView.Columns
If TryCastString(AccountsDataGridView.Rows(gridRow).Cells(gridColumn).Value).ToLower = SearchTextBox.Text.ToLower Then
'AccountsDataGridView.Rows(intcount).Cells(0).Value = "0"
MsgBox("FOUND") 'Should be highlight insted of showing message and the cell should be select.
End If
gridColumn += 1
Next column
gridColumn = 0
gridRow += 1
Next Row
Is there any way to implement my concept? I am using vb.net windows form. Thanks in advance.
有什么方法可以实现我的概念吗?我正在使用 vb.net windows 窗体。提前致谢。
回答by Jay
You could use String.contains instead of an =.
您可以使用 String.contains 而不是 =。
Here is the MSDN article on the contains method:
这是关于 contains 方法的 MSDN 文章:
http://msdn.microsoft.com/en-us/library/dy85x1sa.aspx
http://msdn.microsoft.com/en-us/library/dy85x1sa.aspx
code to style the cell if it contains the search text:
如果单元格包含搜索文本,则用于设置单元格样式的代码:
Dim someText As String = SearchTextBox.Text
Dim gridRow As Integer = 0
Dim gridColumn As Integer = 0
For Each Row As DataGridViewRow In AccountsDataGridView.Rows
For Each column As DataGridViewColumn In AccountsDataGridView.Columns
Dim cell As DataGridViewCell = (AccountsDataGridView.Rows(gridRow).Cells(gridColumn))
If cell.Value.ToString.ToLower.Contains(someText.ToLower) Then
cell.Style.BackColor = Color.Yellow
End If
gridColumn += 1
Next column
gridColumn = 0
gridRow += 1
Next Row
回答by Meta-Knight
Well you could set the cell's background color with a different color to highlight all the matches, and select only the cell corresponding to the current match:
那么您可以将单元格的背景颜色设置为不同的颜色以突出显示所有匹配项,并仅选择与当前匹配项对应的单元格:
Dim searchIndex = 0
AccountsDataGridView.ClearSelection()
For Each row As DataGridViewRow In AccountsDataGridView.Rows
For each cell As DataGridViewCell in row.Cells
If CStr(cell.Value).Contains(SearchTextBox.Text, StringComparison.OrdinalIgnoreCase) Then
If searchIndex = m_CurrentSearchIndex Then
'This is the cell we want to select
cell.Selected = True
End If
'Yellow background for all matches
cell.Style.BackColor = Color.Yellow
searchIndex += 1
End If
Next
Next
If m_CurrentSearchIndex has a value of 0, it would select the first match, second match for a value of 1, etc.
如果 m_CurrentSearchIndex 的值为 0,它将选择第一个匹配项,第二个匹配项的值为 1,依此类推。
回答by alaa
Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
For Each dr As DataGridViewRow In Me.DataGridView1.Rows
If dr.Cells(0).Value.ToString.Contains(TextBox2.Text) Then dr.Visible = True Else dr.Visible = False
Next
End Sub