Results 1 to 7 of 7

Thread: [RESOLVED] filtering a datagridview

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Location
    Southeast Michigan
    Posts
    155

    Resolved [RESOLVED] filtering a datagridview

    I have a datagridview that is filled as follows:

    Code:
    Private Sub cmdExecuteSproc(ByVal SO_Number As String, ByVal Prev_SO_Number As String)
            Dim spSteps As New SqlCommand("sproc_shop_order_get_recipe1", cnPMSQL)
            spSteps.Parameters.AddWithValue("@int_shop_order", SO_Number)
            spSteps.Parameters.AddWithValue("@int_prev_so", Prev_SO_Number)
            spSteps.CommandType = CommandType.StoredProcedure
    
                Dim StepsAdapter As SqlDataAdapter = New SqlDataAdapter(spSteps)
                tblSteps.Clear()
                StepsAdapter.Fill(tblSteps)
        End Sub 'cmdExecuteSproc()
    I'd like to have some checkboxes to determine what rows are visible based on criteria set for certain fields. Any suggestions are greatly appreciated.
    Dave

    Helpful information I've found here so far : The Definitive "Passing Data Between Forms" : Restrict TextBox to only certain characters, numeric or symbolic :
    .NET Regex Syntax (scripting) : .NET Regex Language Element : .NET Regex Class : Regular-Expressions.info
    Stuff I've learned here so far : Bing and Google are your friend. Trying to help others solve their problems is a great learning experience

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: filtering a datagridview

    If you haven't already, bind your DataTable to a BindingSource and then bind that to your grid. You can then set the Filter property of that BindingSource based on whatever criteria you like. I'd suggest having a single method that builds and sets the Filter, then call that method from all the appropriate event handlers, e.g. CheckedChanged of all your CheckBoxes.
    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
    Lively Member
    Join Date
    Apr 2010
    Posts
    74

    Re: filtering a datagridview

    jm, do you have a codebank for filtering?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: filtering a datagridview

    Quote Originally Posted by sushi911 View Post
    jm, do you have a codebank for filtering?
    It's one line of code. Create a string containing the filter and assign it to the BindingSource's Filter property. The filter is basically a SQL WHERE clause but it only supports specific syntax. For details of that syntax you should read the documentation.
    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

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Location
    Southeast Michigan
    Posts
    155

    Re: filtering a datagridview

    Thank you. Works great but just want to know if I've followed the documentation correctly.

    Code:
    Private Sub cmdExecuteSproc(ByVal SO_Number As String, ByVal Prev_SO_Number As String)
            Dim spSteps As New SqlCommand("sproc_shop_order_get_recipe1", cnPMSQL)
            spSteps.Parameters.AddWithValue("@int_shop_order", SO_Number)
            spSteps.Parameters.AddWithValue("@int_prev_so", Prev_SO_Number)
            spSteps.CommandType = CommandType.StoredProcedure
    
                Dim StepsAdapter As SqlDataAdapter = New SqlDataAdapter(spSteps)
                Dim MyBindingSource as As New BindingSource
                tblSteps.Clear()
                StepsAdapter.Fill(tblSteps)
                MyBindingSource.DataSource = tblSteps
                ADataGridView.DataSource = MyBindingSource
                MyBindingSource.Filter = "str_cost_profit_id = 'DS'"
        End Sub 'cmdExecuteSproc()
    Dave

    Helpful information I've found here so far : The Definitive "Passing Data Between Forms" : Restrict TextBox to only certain characters, numeric or symbolic :
    .NET Regex Syntax (scripting) : .NET Regex Language Element : .NET Regex Class : Regular-Expressions.info
    Stuff I've learned here so far : Bing and Google are your friend. Trying to help others solve their problems is a great learning experience

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: filtering a datagridview

    That looks OK to me, although it would be more efficient to set the Filter before setting the DataSource of the grid. That way you don't draw the grid with all the data and then have to redraw it after filtering.

    Also, I'm assuming that you may at some point want to change the filter and display the data that doesn't match that initial condition. If that's not the case then it would make more sense to include that condition in the original query and retrieve less data in the first place.
    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
    Addicted Member
    Join Date
    Mar 2010
    Location
    Southeast Michigan
    Posts
    155

    Re: filtering a datagridview

    Thanks. In the actual program I am setting the filter ahead of time and am changing it.

    As always, I appreciate the help.
    Dave

    Helpful information I've found here so far : The Definitive "Passing Data Between Forms" : Restrict TextBox to only certain characters, numeric or symbolic :
    .NET Regex Syntax (scripting) : .NET Regex Language Element : .NET Regex Class : Regular-Expressions.info
    Stuff I've learned here so far : Bing and Google are your friend. Trying to help others solve their problems is a great learning experience

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