Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind a combobox - dealing with foreign keys

Tags:

c#

combobox

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

like image 597
user1820392 Avatar asked Dec 01 '25 12:12

user1820392


1 Answers

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

like image 150
nflash Avatar answered Dec 03 '25 01:12

nflash



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!