
Introduction
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.
Tutorial
1. Add a DataSource
Open your Form in Design View. Click on Add New Datasource in the Data Sources window.

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.

Set CustomersBindingSource.Position
to the SelectedIndex
as below:
VB:
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
C#:
private void ComboBox1_SelectedValueChanged(object sender, System.EventArgs e)
{
this.CustomersBindingSource.Position = this.ComboBox1.SelectedIndex;
}
Conclusion
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.
Sacramento, CA based Senior .Net and SharePoint Solution Architect for
Kiefer Consulting
(1-800-794-1928)
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.