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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

765 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