PeterBaileyUk
asked on
vb.net searchandselect
My search and select is doing partial matches like an instring so if i put the word 'to' in my textbox and press enter the datagrid has Auto selected as an example. i need a whole match but not sure how to do that.
Private Sub OnKeyUpBulk(sender As Object, e As KeyEventArgs) Handles TxtBoxSearchTag.KeyUp
Dim tb2 = DirectCast(sender, TextBox)
If tb2.Equals(TxtBoxSearchTag) Then
If e.KeyCode.Equals(Keys.Enter) Then
DataGridViewTag.SearchAndSelect(tb2.Text, CBTagSearchDesc.SelectedItem, True)
tb2.Clear()
e.Handled = True
End If
End If
End Sub
ASKER
DataGridViewTag.SearchAndSelect(tb2.Text, CBTagSearchDesc.SelectedItem, False)
this didnt work it gives the same result.
what is this actually...
DataGridViewTag.SearchAndS elect
I never saw it...
DataGridViewTag.SearchAndS
I never saw it...
ASKER
its the way to search in the datagridview object
Is this your own method
DataGridViewTag.SearchAndSelect(tb2.Text, CBTagSearchDesc.SelectedItem, True)
if so please show us the code you have written in that
What the other Experts are saying is that SearchAndSelect is NOT a Method or Function of the standard .NET datagridview control. It is either a custom function, or you're using something other than the native datagridviewcontrol.
Is your DGV attached to a Datatable? If so, then you could use the RowFilter method of that Datatable:
Dim dt As New Datatable = DGV.Datasource
dt.DefaultView.RowFilter = "some_column=" & YourSearchString
DGV.DataSource = dt
If some_column is Text or Date:
dt.DefaultView.RowFilter = "some_column='" & YourSearchString & "'"
If your DGV is bound to something else, like an array or List, then use the filtering methods of that object and do the same thing.
Is your DGV attached to a Datatable? If so, then you could use the RowFilter method of that Datatable:
Dim dt As New Datatable = DGV.Datasource
dt.DefaultView.RowFilter = "some_column=" & YourSearchString
DGV.DataSource = dt
If some_column is Text or Date:
dt.DefaultView.RowFilter = "some_column='" & YourSearchString & "'"
If your DGV is bound to something else, like an array or List, then use the filtering methods of that object and do the same thing.
ASKER
aha ok the penny dropped:
I omitted this bit
I omitted this bit
<Extension()>
Public Sub SearchAndSelect(grid As DataGridView, term As String, selector As String, Optional clearSelected As Boolean = False)
If grid IsNot Nothing AndAlso grid.Rows.Count < 1 Then Return
If String.IsNullOrWhiteSpace(term) Then Return
grid.BeginEdit(True)
Dim counter = 0
If clearSelected Then
For Each row In grid.SelectedRows.Cast(Of DataGridViewRow)()
row.Selected = False
Next
End If
For Each row In grid.Rows.Cast(Of DataGridViewRow)()
If selector <> "- All Columns -" Then
If row.Cells(selector).Value IsNot Nothing AndAlso row.Cells(selector).Value.ToString().IndexOf(term, StringComparison.OrdinalIgnoreCase) <> -1 Then
row.Selected = True
counter += 1
Continue For
End If
Else
For Each column In grid.Columns.Cast(Of DataGridViewColumn)()
If row.Cells(column.Name).Value IsNot Nothing AndAlso row.Cells(column.Name).Value.ToString().IndexOf(term, StringComparison.OrdinalIgnoreCase) <> -1 Then
row.Selected = True
counter += 1
Exit For
End If
Next
End If
Next
grid.EndEdit()
If counter = 0 Then MessageBox.Show(String.Format("No rows were found that matched - {0}", term))
If counter = 0 Then MessageBox.Show(String.Format("No rows were found that matched - {0}", term))
If grid.SelectedRows.Count > 0 Then
grid.FirstDisplayedScrollingRowIndex = grid.SelectedRows.Cast(Of DataGridViewRow).Reverse()(0).Index
End If
End Sub
Can you post a sample screen shot of what you are expecting as output and what is your current output
When you say "whole match", do you mean that you want to only match on an entire word in the cell's contents? Using your example, you'd want a value of "to" to match, but NOT a value of "Auto", "Grantor", "editor", etc?
If so, then I don't know if you can only match entire words. You could split the contents of the cell into an array by the Space character, and then compare that array to your search term, but depending on the contents of your grid that could be a performance dud.
If so, then I don't know if you can only match entire words. You could split the contents of the cell into an array by the Space character, and then compare that array to your search term, but depending on the contents of your grid that could be a performance dud.
ASKER
yes exactly as Scott describes, ok i will leave it as it is performing fine speedwise, I just didnt know if it were possible.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
that did it! Thank you
DataGridViewTag.SearchAndS
try
DataGridViewTag.SearchAndS