Avatar of grouchyfd
grouchyfdFlag for United States of America

asked on 

How do I filter my DataGridView using a ComboBox selection?

I have an ADO.Net app that uses an ODBC connection to a remote database (so I don't need to hardcode in the servername and password). I have one form that uses a DataGridView to show data from a TableView. I need to filter this data using the selected item in a ComboBox. I'm using the SelectedIndexChange method. I can build everything down to the dataset but I cant get the DataGrid view to show any of the selected data. What am I doing wrong?

  Dim Con As New Odbc.OdbcConnection
        Con.ConnectionString = My.Settings.ODBCConnectionString
        Dim Com As New Odbc.OdbcCommand
        Com.Connection = Con
        Com.CommandText = "Select ProductName, CategoryName, Barcode, UnitsInStock, Par " & _
                          "From vProduct " & _
                          "Where ProductName like '" & ComboBox1.SelectedValue & "' and Discontinued = 0 " & _
                          "Order By ProductName"
        Con.Open()
        Dim da As New Odbc.OdbcDataAdapter
        Dim ds As New DataSet

        da.SelectCommand = Com
        da.Fill(ds, "Product")
        VProductDataGridView.DataSource = ds
        VProductDataGridView.DataMember = "Product"

Thanks
.NET ProgrammingVisual Basic.NET

Avatar of undefined
Last Comment
grouchyfd
Avatar of VBRocks
VBRocks
Flag of United States of America image

First, change your code to use a DataView:

VProductDataGridView.DataSource = ds.Tables("Product")

Then to filter it:

        Dim view As DataView = Me.DataGridView1.DataSource

        view.RowFilter = "ColumnName='" & ComboBox.Text & "'"


That's it.


To remove the filter:

        Dim view As DataView = Me.DataGridView1.DataSource

        view.RowFilter = ""

ASKER CERTIFIED SOLUTION
Avatar of VBRocks
VBRocks
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of grouchyfd
grouchyfd
Flag of United States of America image

ASKER

Excellent. Works great. Thankyou.
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo