Click here to Skip to main content
Click here to Skip to main content
Go to top

Filter a DataGrid using a Foreign Key Combobox

, 15 Jan 2007
Rate this:
Please Sign up or sign in to vote.
Filter a DataGrid using a Foreign Key combobox - VS 2005 RAD (.NET 2.0).

Sample Image - ComboboxGridFilter.jpg

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.

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.

ConfigureNorthwindConnection

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:

ChoosePKFKTables

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.

DragDropCombobox

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

SetComboboxDatasource

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

SetComboboxDisplaymember

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

SetComboboxValueMember

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.

DragDropGrid

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:

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.

License

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

Share

About the Author

Robert R Freeman
Chief Technology Officer Kiefer Consulting
United States United States
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.

Comments and Discussions

 
GeneralMy vote of 5 Pinmembersariqkhan21-Nov-12 0:13 
GeneralMy vote of 1 PinmembergisTimmy3-Dec-08 10:45 
GeneralFilter a Datagrid using a Foreign Key Combobox - VS 2005 RAD (.net 2.0) PinmemberOMurgan25-Apr-08 5:10 
GeneralRe: Filter a Datagrid using a Foreign Key Combobox - VS 2005 RAD (.net 2.0) PinmemberRobert R Freeman25-Apr-08 5:42 
GeneralRe: Filter a Datagrid using a Foreign Key Combobox - VS 2005 RAD (.net 2.0) Pinmemberjisty_guy23-Feb-10 20:56 
GeneralTwo combobox's PinmemberOlli H15-May-07 19:55 
GeneralRe: Two combobox's PinmemberRobert R Freeman16-May-07 5:39 
Generalsuggestion Pinmemberblakey40421-Mar-07 2:08 
GeneralRe: suggestion PinmemberRobert R Freeman21-Mar-07 7:36 
GeneralRe: suggestion PinmemberHorgey14-Nov-07 16:25 
QuestionC#?? PinmemberMuammar©12-Jan-07 22:58 
AnswerRe: C#?? PinmemberRobert R Freeman22-Jan-07 9:31 
GeneralRe: C#?? PinmemberMuammar©22-Jan-07 19:18 
GeneralRe: C#?? PinmemberRobert R Freeman24-Jan-07 12:06 
GeneralSugestion PinmemberPop Catalin21-Mar-06 0:20 
AnswerRe: Sugestion PinmemberRobert R Freeman21-Mar-06 8:26 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 15 Jan 2007
Article Copyright 2006 by Robert R Freeman
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid