Click here to Skip to main content
15,611,775 members
Articles / Programming Languages / Visual Basic
Posted 20 Mar 2006


28 bookmarked

Filter a DataGrid using a Foreign Key Combobox

Rate me:
Please Sign up or sign in to vote.
1.78/5 (10 votes)
15 Jan 2007CPOL2 min read
Filter a DataGrid using a Foreign Key combobox - VS 2005 RAD (.NET 2.0).

Sample Image - ComboboxGridFilter.jpg


The following simple WinForms tutorial reveals the fastest way to create a foreign key combobox filter for a DataGrid in Visual Studio 2005. We will be using the Access NorthWind database as our data source. If you do not have the NorthWind database, you can download it here.


1. Add a DataSource

Open your Form in Design View. Click on Add New Datasource in the Data Sources window.

Add Data Source

Choose Database as the Datasource Type. Select Microsoft Access Database File (OLE DB) as the Data Source. Browse to the location of the Northwind database. Usually: C:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb.


Click Next till you get to Choose Your Database Objects. Select CustomerID and ContactName from the Customers table, and select the entire Orders table as below:


Click Finish.

Note: A Foreign Key Relationship is automatically added to the dataset since the relation was defined in the database. If you are using a non-relational database, you will need to add this relation to the dataset manually.

2. Create the Combobox

Drag a ComboBox from the Toolbox onto the form.


Select the combobox and go to the Properties window. Set the Datasource to the Customers table.


Set the DisplayMember to ContactName. This will determine the combobox's text.


Set the ValueMember to CustomerID. This will determine the combobox's selected value.


3. Create the Grid

Drag the Orders table from the Datasources window onto the form. This will create a DataGrid bound to the Orders table and will add an Orders BindingSource to the form.


4. Code the ValueChanged Handler

Select the Combobox. Click the Event Icon (lightning bolt) in the Properties window. Double click on the SelectedValueChanged event to create and go to the SelectedValueChanged handler in the code.

Selected Value Changed Handler

Set CustomersBindingSource.Position to the SelectedIndex as below:


Private Sub ComboBox1_SelectedValueChanged(ByVal sender As System.Object, _ 
        ByVal e As System.EventArgs) Handles ComboBox1.SelectedValueChanged
    Me.CustomersBindingSource.Position = Me.ComboBox1.SelectedIndex
End Sub


private void ComboBox1_SelectedValueChanged(object sender, System.EventArgs e) 
    this.CustomersBindingSource.Position = this.ComboBox1.SelectedIndex; 


We now have an order history filtered by customer in a matter of minutes. This method works with any relational datasource.

Please comment on anything that needs further clarification, or with any questions on implementing this. Also, please provide suggestions before rating the article less than a 5.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Chief Technology Officer Kiefer Consulting
United States United States
Sacramento, CA based Senior .Net and SharePoint Solution Architect for Kiefer Consulting
B.S. in Mathematics from UCDavis
.NET Wizard - Experts-Exchange
MCSD, MCTS: MOSS 2007 Config

Some of the bigger questions:
1. What is the meaning of my life?
To satisfy the purpose of your creator(s). (Meaning must derive from purpose. Those who create you, give you meaning.)

2. Who is my creator?
Ultimately, God is your creator. God designed and created the universe and everything in it. You and others in your life can also be a part of your creation, overriding or furthering God's purpose.

3. What is God's purpose for me?
To love and be loved by your creator and others and to enjoy the life you've been given.
This can be distinguished two ways.
a. Use your built in common sense (morality/feelings)
b. Use the creator's handbook. Fortunately our creator did not abandon us. He is with us now and even lived and died as one of us. Check out his biography in "The Bible"

Note on free-will vs. predetermination:
God exists outside the constraints of time. He exists at every point in time simultaneously and knew of your birth and every decision you will/have made. But this does not mean God predetermined any of it. Pre and post are time related concepts that do not apply to God. God always has been and always will be. He determines our universe to exist, gives it the parameters of natural law, and allows us to make our own way through it (free-will). Note that these are all present tense, it would be more appropriate to use past, present, and future tense combined. God's purpose is for us to love him and one another. But a prerequisite of love is the free-will to love. So even though God wants us to love him/others, he can not ensure it. He can, however, help us if we allow him to. A miracle is God modifying natural parameters in response to human will.

Comments and Discussions

GeneralMy vote of 5 Pin
sariqkhan21-Nov-12 0:13
sariqkhan21-Nov-12 0:13 
GeneralMy vote of 1 Pin
gisTimmy3-Dec-08 10:45
gisTimmy3-Dec-08 10:45 
GeneralFilter a Datagrid using a Foreign Key Combobox - VS 2005 RAD (.net 2.0) Pin
spmspm25-Apr-08 5:10
spmspm25-Apr-08 5:10 
GeneralRe: Filter a Datagrid using a Foreign Key Combobox - VS 2005 RAD (.net 2.0) Pin
Robert R Freeman25-Apr-08 5:42
Robert R Freeman25-Apr-08 5:42 
GeneralRe: Filter a Datagrid using a Foreign Key Combobox - VS 2005 RAD (.net 2.0) Pin
jisty_guy23-Feb-10 20:56
jisty_guy23-Feb-10 20:56 
GeneralTwo combobox's Pin
Olli H15-May-07 19:55
Olli H15-May-07 19:55 
GeneralRe: Two combobox's Pin
Robert R Freeman16-May-07 5:39
Robert R Freeman16-May-07 5:39 
Generalsuggestion Pin
blakey40421-Mar-07 2:08
blakey40421-Mar-07 2:08 
GeneralRe: suggestion Pin
Robert R Freeman21-Mar-07 7:36
Robert R Freeman21-Mar-07 7:36 
GeneralRe: suggestion Pin
Horgey14-Nov-07 16:25
Horgey14-Nov-07 16:25 
QuestionC#?? Pin
Muammar©12-Jan-07 22:58
Muammar©12-Jan-07 22:58 
AnswerRe: C#?? Pin
Robert R Freeman22-Jan-07 9:31
Robert R Freeman22-Jan-07 9:31 
GeneralRe: C#?? Pin
Muammar©22-Jan-07 19:18
Muammar©22-Jan-07 19:18 
GeneralRe: C#?? Pin
Robert R Freeman24-Jan-07 12:06
Robert R Freeman24-Jan-07 12:06 
GeneralSugestion Pin
Pop Catalin21-Mar-06 0:20
Pop Catalin21-Mar-06 0:20 
AnswerRe: Sugestion Pin
Robert R Freeman21-Mar-06 8:26
Robert R Freeman21-Mar-06 8:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.