Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
I'm new to C# but not to programming. I've been toying around with VS, becoming more comfortable with the language and got stuck on a combobox bound to an int datatable column of an in-memory dataset.

I worked on a DataGridView and had no problems creating a DataGridViewComboBoxColumn and populating its dropdown list using a custom class comprised of Key:int and Desc:string. ValueMember = Key, DisplayMember = Desc. DataSource is set to a BindingList<> populated with the list objects.  Pick from the combobox and save changes works fine in the grid.

Then I moved to a ComboBox.  Both the grid and the ComboBox have the exact same BindingSource. So yes they point to the same row at the same time.

I bind the ComboBox with

cbCtrl.DataBindings.Add( "Text", myBindingSource, ColDBName );

Without populating the dropdown items of the ComboBox, I see the raw int value show up in the ComboBox.  I change rows using the grid and the ComboBox value changes too.  So it's good.

Then I hook up BindingList<> to provide the dropdown list of items in the ComboBox (the same as the grid's column tied to the same column) and it's trouble.  The raw int value (eg 19) does not get matched to the proper "Key" item in the dropdown list of the ComboBox.  I've tried DropDownStyle = DropDown & DropDownList but no luck with either.  And I've looked for hours trying to find a fix.  There are no events hooked up to the ComboBox.  I exepcted automatic hookup of the raw value to the matching "Key" item in the ComboBox's dropdown list.

Is there something else to hookup?  Frustrating since I expect something minor.

Any help would be GREATLY appreciated.  Thank you.


What I have tried:

I also directly populated the ComboBox with objects instead of hooking up the BindingList<> to the DataSource but the behavior is the same.
Posted
Updated 12-Nov-16 12:55pm

1 solution

I finally stumbled on the answer:

cbCtrl.DataBindings.Add( "SelectedValue", myBindingSource, ColDBName );

That's all I needed. I got it from a great article
A Detailed Data Binding Tutorial[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900