问题描述
我有一个名为GridViewForm的窗口窗体中的数据网格视图。当用户从名为FindForm的另一窗口窗体搜索框中搜索文本时,我想突出显示数据网格视图中的所有匹配结果。搜索类型可以是准确的或部分的。
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.
对于例如
如果用户搜索文本堆栈,则应该突出显示来自[堆栈,堆栈,堆栈,堆栈交换]的单词stack,并且应该选择与查询匹配的第一个单元格。当用户按下下一个按钮时,应该选择与搜索查询匹配的另一个单元格。
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.
找到文本的代码就像跟随它仅搜索确切的单词。
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
有没有办法实现我的概念?我正在使用vb.net窗体。感谢提前。
Is there any way to implement my concept? I am using vb.net windows form. Thanks in advance.
推荐答案
您可以使用不同的颜色设置单元格的背景颜色,以突出显示所有匹配,并仅选择对应于当前匹配的单元格:
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
如果m_CurrentSearchIndex的值为0,那么它会选择第一个匹配,第二个匹配值为1等。
If m_CurrentSearchIndex has a value of 0, it would select the first match, second match for a value of 1, etc.
这篇关于从vb.net中的datagrid视图查找文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!