[RESOLVED] Datagridview Search through all Columns and Rows?
Is there a way to search all columns and rows in a datagridview? My goal is to have a Search Textbox and Find button. User will enter search string in the textbox and press the find button to search. If found the row will be selected. It would be nice if the button will also have the capability to find next.
Right now I have this code below. It can only search for a specific column in the datagridview. It would be nice if it can traverse all columns.
Code:
Private Function FindString(ByVal strSearchString As String, ByVal strFields As String) As Boolean
dgvDisbursements.SelectionMode = DataGridViewSelectionMode.FullRowSelect
dgvDisbursements.ClearSelection()
Dim intCount As Integer = 0
For Each row As DataGridViewRow In dgvDisbursements.Rows
If InStr(1, dgvDisbursements.Rows(intCount).Cells(strFields).Value.ToString, strSearchString, CompareMethod.Text) Then
dgvDisbursements.Rows(intCount).Selected = True
FindString = True
Exit Function
End If
intCount += 1
Next row
FindString = False
End Function
Re: Datagridview Search through all Columns and Rows?
Hi again
Code:
For Each myRow As DataGridViewRow In dgvDisbursements.Rows
For Each myCell in myRow.Cells
'Do the search in myCell
Next
Next
I did not explicitly declare the type of myCell as it will be DataGridViewTextBoxCell or DataGridViewCheckBoxCell
Re: Datagridview Search through all Columns and Rows?
Quote:
Originally Posted by
kaliman79912
Hi again
Code:
For Each myRow As DataGridViewRow In dgvDisbursements.Rows
For Each myCell in myRow.Cells
'Do the search in myCell
Next
Next
I did not explicitly declare the type of myCell as it will be DataGridViewTextBoxCell or DataGridViewCheckBoxCell
Again tnx Kaliman79912 for helping me out. I will try this later. Need to go to sleep (1:30 am).
Re: Datagridview Search through all Columns and Rows?
Quote:
Originally Posted by
kaliman79912
Hi again
Code:
For Each myRow As DataGridViewRow In dgvDisbursements.Rows
For Each myCell in myRow.Cells
'Do the search in myCell
Next
Next
I did not explicitly declare the type of myCell as it will be DataGridViewTextBoxCell or DataGridViewCheckBoxCell
thanks kaliman79912. It took me awhile to get it to work.
My code:
Code:
Private Function FindItems(ByVal strSearchString As String) As Boolean
dgvDisbursements.SelectionMode = DataGridViewSelectionMode.FullRowSelect
dgvDisbursements.ClearSelection()
Dim intCount As Integer = 0
Dim intCell As Integer
For Each myRow As DataGridViewRow In dgvDisbursements.Rows
For Each myCell In myRow.Cells
For intCell = 1 To 8
'Do the search in myCell
If InStr(1, dgvDisbursements.Rows(intCount).Cells(intCell).Value.ToString, strSearchString, CompareMethod.Text) Then
dgvDisbursements.Rows(intCount).Selected = True
FindItems = True
Exit Function
End If
Next
intCell += 1
Next
intCount += 1
Next
Return False
End Function
Re: [RESOLVED] Datagridview Search through all Columns and Rows?
Hello again. I do not know if you are going to read this post as you already marked it [RESOLVED], but I think you should note some issues.
I will tell you what I think you are not doing right and then propose a new solution.
First of all, I think you missed the whole point of the nested loops, For Each myRow & For Each myCell do a sweep of all the cells in the DataGridView, you do not need to add two counters and then do another For intCell inside it.
Then, after the For intCell loop finishes you add one to intCell but return to the loop, that line does nothing there.
I included in my example the two lines that configure dgvDisbursements parameters, but I think those should be in the form load or set in the designer.
Look at this code, it does exactly what yours.
Code:
Private Function FindItems(ByVal strSearchString As String) As Boolean
dgvDisbursements.SelectionMode = DataGridViewSelectionMode.FullRowSelect
dgvDisbursements.ClearSelection()
For Each myRow As DataGridViewRow In dgvDisbursements.Rows
For Each myCell As DataGridViewCell In myRow.Cells
If InStr(myCell.Value.ToString, strSearchString) Then
myRow.Selected = True
Return True
End If
Next
Next
Return False
End Function
Re: [RESOLVED] Datagridview Search through all Columns and Rows?
Quote:
Originally Posted by
kaliman79912
Hello again. I do not know if you are going to read this post as you already marked it [RESOLVED], but I think you should note some issues.
I will tell you what I think you are not doing right and then propose a new solution.
First of all, I think you missed the whole point of the nested loops, For Each myRow & For Each myCell do a sweep of all the cells in the DataGridView, you do not need to add two counters and then do another For intCell inside it.
Then, after the For intCell loop finishes you add one to intCell but return to the loop, that line does nothing there.
I included in my example the two lines that configure dgvDisbursements parameters, but I think those should be in the form load or set in the designer.
Look at this code, it does exactly what yours.
Code:
Private Function FindItems(ByVal strSearchString As String) As Boolean
dgvDisbursements.SelectionMode = DataGridViewSelectionMode.FullRowSelect
dgvDisbursements.ClearSelection()
For Each myRow As DataGridViewRow In dgvDisbursements.Rows
For Each myCell As DataGridViewCell In myRow.Cells
If InStr(myCell.Value.ToString, strSearchString) Then
myRow.Selected = True
Return True
End If
Next
Next
Return False
End Function
Thanks Kaliman79912 for the tutorial. Your help is greatly appreciated.
Re: [RESOLVED] Datagridview Search through all Columns and Rows?
Code:
Private Function FindItems(ByVal strSearchString As String) As Boolean
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
DataGridView1.ClearSelection()
MsgBox(strSearchString)
For Each myRow As DataGridViewRow In DataGridView1.Rows
For Each myCell As DataGridViewCell In myRow.Cells
If InStr(myCell.Value.ToString, strSearchString) Then
myRow.Selected = True
Return True
End If
Next
Next
Return False
End Function
At the above hilighted line,I am getting an "Object Reference Not set to an object error"
What am I doing wrong here?
Thanks,
Khalid.