user1820392
user1820392

Reputation: 39

Bind a combobox - dealing with foreign keys

In my database I have a table Cars which contains (among other things) an idCategory column which is a foreign key. That foreign key refers idCategory in the Category table which contains the description of the category.

Now in my C# application I got a form that is used to add new cars. There is a combobox to choose the category.

comboboxCat.DataSource = datatable1;
comboboxCat.DisplayMember = "categoryDescription";
comboboxCat.ValueMember = "idCategory";

So I set the DataSource of the ComboBox to the DataTable that contains the data of Category table, and I set the id as the valueMember and the Description as DisplayMember.

I also have a DataSet containing the table Cars. What I want to do is to automatically update the Car data in the DataSet when the value of the Combobox is changed. I don't know how to bind it. In a datagridview it's easy because I just have to set the DataPropertyName of the DataGridViewComboBoxColumn. But there's not such properties in a normal ComboBox. Thanks in advance

Upvotes: 3

Views: 4285

Answers (2)

user1820392
user1820392

Reputation: 39

Finally found what I was looking for : http://blogs.msdn.com/b/bethmassi/archive/2007/04/25/tips-on-related-data-binding-and-comboboxes.aspx

My answer is to use DataBindings to make the link

Upvotes: 0

nflash
nflash

Reputation: 430

I belive you have to bind the IdCategory field from the Cars DataTable to the SelectedValue of the ComboBox.

Upvotes: 1

Related Questions