Filter datagrid with combobox

Posted on 2005-03-07
Medium Priority
Last Modified: 2008-03-06

I am writing a VB.NET application.  On the form I have a combobox called cboClients and a DataGrid called dGridJobs.  I want to filter the contents in the datagdrid by using the data in the combobox.  Can anyone give me an example on how to do this?

Question by:Vyyk_Drago
  • 2

Accepted Solution

Koala119 earned 1600 total points
ID: 13475925
If I am not wrong, you can try:

Suppose you want to filter the datagrid according to a particular column which is specified in the combobox, add the following in the combobox selectedIndexChanged:

   Private Sub cboClients _SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboClients .SelectedIndexChanged
        ' Get the dataview of the datagrid, but I think you may have a global datatable that storing all the contents?
         Dim dv As DataView = CType(dGridJobs.DataSource, DataTable).DefaultView()

        With dv
            .RowFilter = "TheColumnNameYouWantToFilter = '" & cboClients .SelectedItem & "'"
            .RowStateFilter = DataViewRowState.CurrentRows
        End With

       ' Bind the dv to the datagrid
        dGridJobs.DataSource = dv
    End Sub

After set the  dGridJobs.DataSource = dv, the datagrid will be filtered.
The Dim dv As DataView = CType(dGridJobs.DataSource, DataTable).DefaultView() may need to be changed to get a global datatable.defaultview, otherwise it will always filter the filetered grid.
LVL 10

Assisted Solution

123654789987 earned 400 total points
ID: 13475948
U can refer the following links


Author Comment

ID: 13476823
Thanks Koala, but I get the folloing error on this line:

'Generates error: Specified Cast is not Valid
Dim dv As DataView = CType(dGridJobs.DataSource, DataTable).DefaultView()

Any ideas - looks like we are almost there...

Author Comment

ID: 13477076
Cheers - extra bit from 123456789987 helped me the last bit of the way.  Many thanks...

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

616 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question