-
Nov 23rd, 2013, 11:52 AM
#1
Thread Starter
Addicted Member
[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
-
Nov 23rd, 2013, 12:14 PM
#2
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
Last edited by kaliman79912; Nov 23rd, 2013 at 12:18 PM.
More important than the will to succeed, is the will to prepare for success.
Please rate the posts, your comments are the fuel to keep helping people
-
Nov 23rd, 2013, 12:31 PM
#3
Thread Starter
Addicted Member
Re: Datagridview Search through all Columns and Rows?
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).
-
Nov 24th, 2013, 05:48 AM
#4
Thread Starter
Addicted Member
Re: Datagridview Search through all Columns and Rows?
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
-
Nov 25th, 2013, 09:57 AM
#5
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
Last edited by kaliman79912; Nov 25th, 2013 at 10:08 AM.
Reason: Code formatting
More important than the will to succeed, is the will to prepare for success.
Please rate the posts, your comments are the fuel to keep helping people
-
Nov 30th, 2013, 09:03 AM
#6
Thread Starter
Addicted Member
Re: [RESOLVED] Datagridview Search through all Columns and Rows?
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.
-
Jan 1st, 2015, 02:46 AM
#7
New Member
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.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|