Results 1 to 7 of 7

Thread: [RESOLVED] Datagridview Search through all Columns and Rows?

  1. #1

    Thread Starter
    Addicted Member coolwater's Avatar
    Join Date
    Dec 2004
    Location
    philippines
    Posts
    215

    Resolved [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

  2. #2
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    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

  3. #3

    Thread Starter
    Addicted Member coolwater's Avatar
    Join Date
    Dec 2004
    Location
    philippines
    Posts
    215

    Re: Datagridview Search through all Columns and Rows?

    Quote Originally Posted by kaliman79912 View Post
    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).

  4. #4

    Thread Starter
    Addicted Member coolwater's Avatar
    Join Date
    Dec 2004
    Location
    philippines
    Posts
    215

    Re: Datagridview Search through all Columns and Rows?

    Quote Originally Posted by kaliman79912 View Post
    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

  5. #5
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    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

  6. #6

    Thread Starter
    Addicted Member coolwater's Avatar
    Join Date
    Dec 2004
    Location
    philippines
    Posts
    215

    Re: [RESOLVED] Datagridview Search through all Columns and Rows?

    Quote Originally Posted by kaliman79912 View Post
    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.

  7. #7
    New Member
    Join Date
    Aug 2014
    Posts
    5

    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
  •  



Click Here to Expand Forum to Full Width