Advertisement

03.27.2008 at 10:20AM PDT, ID: 23274858
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

DataGridView filter by Combo Box

Tags: VB.NET
I have viewed tons of the listing for filtering by a datagridview but none seem to fix my problem. I have a Windows application that I am trying to filter by a combo box. The combo box is programmaticly bound to a data set for one table. I have the datagridview programmaticly bound to a data set for another table. I am tring to filter the records in the datagridview by the user id in the combo box. What is the easiest way to do this?
1:
2:
3:
Loads the datagridview from the data set 
dsv = LoadMaster()
        DataGridView1.DataSource = dsv.Tables(0)
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: mdweaver
Solution Provided By: VBRocks
Participating Experts: 1
Solution Grade: A
Views: 321
Translate:
Loading Advertisement...
03.27.2008 at 10:30AM PDT, ID: 21223860

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.27.2008 at 10:38AM PDT, ID: 21223942

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.27.2008 at 10:57AM PDT, ID: 21224147

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.27.2008 at 11:10AM PDT, ID: 21224255

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.27.2008 at 11:21AM PDT, ID: 21224338

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.27.2008 at 11:37AM PDT, ID: 21224475

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.27.2008 at 11:54AM PDT, ID: 21224634

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.27.2008 at 12:08PM PDT, ID: 21224747

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.27.2008 at 12:10PM PDT, ID: 21224761

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.27.2008 at 12:15PM PDT, ID: 21224808

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.27.2008 at 03:03PM PDT, ID: 21226234

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.27.2008 at 03:13PM PDT, ID: 21226289

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.28.2008 at 06:09AM PDT, ID: 21229989

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
03.27.2008 at 10:30AM PDT, ID: 21223860

Rank: Sage

If you are programmatically binding the DataGridView, are you binding it to a DataTable, DataView or
BindingSource?  If you are bidning it to a DataTable, change it to a DataView, for example:

     Me.DataGridView.DataSource = myDataTable.DefaultView


Then, on the CombBoxes TextChanged event, filter your grid:

    Private Sub ComboBox1_TextChanged(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles ComboBox1.TextChanged

        Dim view As DataView = Me.DataGridView1.DataSource
        'view.RowFilter = "ColName='" & Me.ComboBox1.Text & "'"

        view.RowFilter = Me.ComboBox1.DisplayMember & "='" & Me.ComboBox1.Text & "'"

        'or
        'view.RowFilter = "ColName LIKE '%" & Me.ComboBox1.Text & "%'"

    End Sub



        'or
        view.RowFilter = "ColName LIKE '%" & Me.ComboBox1.Text & "%'"
 
03.27.2008 at 10:38AM PDT, ID: 21223942

Rank: Sage

Here's a more generic example for you:

    Private Sub ComboBox1_TextChanged(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles ComboBox1.TextChanged

        Dim list As System.ComponentModel.IBindingListView

        If DataGridView1.DataSource.GetType() Is GetType(DataTable) Then
            list = DirectCast(Me.DataGridView1.DataSource, DataTable).DefaultView
        Else
            list = Me.DataGridView1.DataSource
        End If

        Dim filter As String = Me.ComboBox1.DisplayMember & "='" & Me.ComboBox1.Text & "'"
        list.Filter = filter

    End Sub

 
03.27.2008 at 10:57AM PDT, ID: 21224147
I actually use a stored proceedure to pull in the information I need.  It is a function that loads it as a dataset that is loaded into the datagridview.


 
03.27.2008 at 11:10AM PDT, ID: 21224255
Ok I changed it to:
dim dt as new datatable
dsv = LoadMaster()
        DataGridView1.DataSource = dsv.Tables(0)
        dt = DataGridView1.DataSource
        Me.DataGridView1.DataSource = dt.DefaultView

the combo box changed event is

Dim view As DataView = Me.DataGridView1.DataSource
        view.RowFilter = "colName="" & me.cbEmployee.text& """
        view.RowFilter = Me.cbEmployee.DisplayMember & "=""& ME.cbEmployee.text &"""

I am now getting a "NullReferenceException"
Object reference not set to an instance of an object
any idea?

 
03.27.2008 at 11:21AM PDT, ID: 21224338

Rank: Sage

Sure, make a couple of changes.

First, all you need is:

dsv = LoadMaster()
        DataGridView1.DataSource = dsv.Tables(0).DefaultView


Then, instead of using the TextChange event, go ahead and use the Leave event, this is better,
because it will only get fired 1 time.

    Private Sub ComboBox1_Leave(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles ComboBox1.Leave
 
        'Validation
        If Me.DataGridView1.DataSource Is Nothing Then MsgBox("DataSource Not Set")
        if ME.cbEmployee.text.Trim() = "" Then MsgBox("No text entered in cbEmployee")
        If Me.cbEmployee.DisplayMember.Trim() = "" Then MsgBox("There is not Display Member")

        'Filtering
        Dim view As DataView = Me.DataGridView1.DataSource
        view.RowFilter = Me.cbEmployee.DisplayMember & "=""& ME.cbEmployee.text &"""

    End Sub


Try that and let me know how it works for you.

 
03.27.2008 at 11:37AM PDT, ID: 21224475
I made the following changes and still the datagridview is not reponding to the combo box. If I click into the datagridview it throws a syntaxerror exception

on the view.RowFilter = Me.cbEmployee.DisplayMember & "=""& ME.cbEmployee.text &"""
it says that it cannot interpret tken "" at position 9
 
03.27.2008 at 11:54AM PDT, ID: 21224634

Rank: Sage

Ok, you need to find out what the problem is.

Basically, the RowFilter needs to know:  
     1.  What column do you want to filter?  
     2.  How do you want to filter it?

Run your program, and in the DataGridView, look at the Column Header of the column you want to
filter and see what it says.  If you have not changed it, then it should be the actual name of the
column.  That is what you need to use as the column name in the filter.

Modify this code, run the progam, and see what messages you get:
   MsgBox (Me.cbEmployee.DisplayMember)    '< Does this match the name of the column to filter?
   MsgBox (ME.cbEmployee.text)   '< Is there a valid value here?
   MsgBox (view.Count)   '< Are there any rows?

   'view.RowFilter = Me.cbEmployee.DisplayMember & "=""& ME.cbEmployee.text &"""

Let me know what you get.
 
03.27.2008 at 12:08PM PDT, ID: 21224747
Ok I had orginally changed the heading to be more user friendly. I changed it back to match the column that it is looking for. Ran it and still same error resulted.

The code I ran showed me the following results.

MsgBox (Me.cbEmployee.DisplayMember) this matched
MsgBox (ME.cbEmployee.text)  value of 1
MsgBox (view.Count) showed total count of data set 274
 
03.27.2008 at 12:10PM PDT, ID: 21224761

Rank: Sage

Ok, so what is the name of the column you want to search, and what type of data is it?

 
03.27.2008 at 12:15PM PDT, ID: 21224808
Column is: User_ID and it is varchar(20) example (jdoe)

 
03.27.2008 at 03:03PM PDT, ID: 21226234

Rank: Sage

Great.  Find a valid ID, and then Try this, just to see if it works:

Dim validID as String = "your valid id"  '< replace 'your valid id' with a valid id

view.RowFilter = "User_ID='" & validID & "'"

 
03.27.2008 at 03:13PM PDT, ID: 21226289

Rank: Sage

Here is a little example I made for you.  It is a very simple filtering demo:

   http://www.ee-stuff.com/Expert/Upload/getFile.php?fid=7015

Notice each item in the "Item" column has a number, such as:  "Item 1", "Item 2", "Item 2"

Just enter a number, such "3", and then press the "Filter" button.



Accepted Solution
 
03.28.2008 at 06:09AM PDT, ID: 21229989
That helped Thank you!
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628