dcsimg
Results 1 to 10 of 10

Thread: Search in DatagridView + "search next" feature

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Posts
    13

    Exclamation Search in DatagridView + "search next" feature

    I found the code below to do datagridview searching :
    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
    I need to add "Search Next" which does the searching and row selecting for all found strings in the datagridview.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,634

    Re: Search in DatagridView + "search next" feature

    If you're saying that you want to select the next matching row after the currently selected row then you should use a For loop rather than a For Each loop. That way, you can get the index of the currently selected row, increment that and use the result as the starting point for the loop.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Fanatic Member
    Join Date
    Nov 2016
    Location
    Slovenia
    Posts
    544

    Re: Search in DatagridView + "search next" feature

    I need to add "Search Next" which does the searching and row selecting for all found strings in the datagridview.
    If you're searching string matches on each row in no matter what column than use DefaultRowFilter, much better & easy. Just add Textbox for writing desired strings and create query for filter. You'll get results in grid instantly.

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Posts
    13

    Re: Search in DatagridView + "search next" feature

    Quote Originally Posted by jmcilhinney View Post
    If you're saying that you want to select the next matching row after the currently selected row then you should use a For loop rather than a For Each loop. That way, you can get the index of the currently selected row, increment that and use the result as the starting point for the loop.
    Would you please help me with coding what u said ?

  5. #5
    New Member
    Join Date
    Mar 2018
    Posts
    7

    Re: Search in DatagridView + "search next" feature

    Here's an example on how to accomplish that with a button and a textbox, when filtering, all applicable results related to the search will appear on the datagrid.

    Code:
    Private Sub FilterButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FilterButton.Click
            source1.Filter = "[Item Code] = '" & TextBox1.Text & "'"
            DataGridView1.Refresh()
    End Sub
    hope it helps.
    Last edited by dday9; Mar 7th, 2018 at 09:43 AM.

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,634

    Re: Search in DatagridView + "search next" feature

    I did help you with coding. What I'm not doing is writing the code for you. Try to implement what I suggested and, if it doesn't work, show us what you did and tell us what it did and then we can help you fix it. I'm more than happy to help but I want to see you make an effort first. Anyone can sit back and let others write their code for them.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Posts
    13

    Re: Search in DatagridView + "search next" feature

    Quote Originally Posted by jmcilhinney View Post
    I did help you with coding. What I'm not doing is writing the code for you. Try to implement what I suggested and, if it doesn't work, show us what you did and tell us what it did and then we can help you fix it. I'm more than happy to help but I want to see you make an effort first. Anyone can sit back and let others write their code for them.
    Thanks for your positive intention, but If I had known to code such a thing, I would have never asked your help. I tried , but I can't even put what you suggested into a piece of code. I'm newbie

  8. #8
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,431

    Re: Search in DatagridView + "search next" feature

    Hi charlie,

    how do you fill the DGV, before you start the search ?

    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  9. #9

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Posts
    13

    Re: Search in DatagridView + "search next" feature

    Quote Originally Posted by ChrisE View Post
    Hi charlie,

    how do you fill the DGV, before you start the search ?

    regards
    Chris
    DGV.Columns.Add()
    DGV.Rows.Add()

  10. #10
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,431

    Re: Search in DatagridView + "search next" feature

    Quote Originally Posted by charlie-j View Post
    DGV.Columns.Add()
    DGV.Rows.Add()
    well thanks for the detailed answer, now I know that the data you fill could be a coming from a Database;CSV-File;Text-File;XML-File etc...

    here my detailed answer

    Code:
    'run to the end of the DGV rows
            	For i = 0 To dgv.Rows.Count - 1
        'run to the end of the DGV Columns
                	For g = 0 To dgv.Columns.Count - 1
        ' your code here....
    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width