0
votes

I encountered a very specific behave on the Comboboxes in Winform. I found a lot of quite similar questions, but no useful anwser to my problem:

I have a form where controls are bound to a SQL datasource via TableAdapter/manager. Some of the values are linked to Lookuptables which are of type Combobox. When I first fill the base dataset, all is shown correctly. I can choose items from Combobox (fields id and NamedItem) where "id" is bound as datamember to the base datset table and "NamedItem" is the DisplayMember of the Combobox.

No problems when changing values or updating but ...

once I have to refill the Combobox underlaying datasource (eg. in case, an entry was added), the selected Index of the Combobox is set to 0 - means, the first item in the list.

Sure, I could remember the selectedindex and restore it after. For one Combobox, no problem, but for dozens ...

Is there any other way to keep the last selecteditem after refilling the combobox?

1
Before filling the combobox get the selected item. Then fill comboxbox and set the selected item back to the original; selected item. - jdweng
No there isn't. Think about it. If you are refilling the DataTable then the item that was previously selected no longer exists, so how could it possibly still be selected? If you want the same item to be selected then that item has to still be there, so don't remove it in the first place. If there are new items to be added, just add them instead of throwing out what you already have. - jmcilhinney
If all ComboBox Controls are bound to the same source of data, then change Current, the Data Row your data source points to. All bound Controls will reflect the new position. - Jimi
@jdweng Yes, thats what I am doing now. And I am thniking also meanwhile, it will be the only way. - Werner
@jmcilhinney: no, the items are still there, just one could have been added. And the insert takes place directly in the underlaying SQL DB. Thats why I have to "update" the Combobox. Point is, I am not using DataTableManagers for the lookup tables (coz in this case, a lot of code generated for nothing). Thats why I do the item add in thd DB and then refill the Combobox. Anyhow, I think question is solved. Thx ! - Werner

1 Answers

0
votes

If items would not be deleted from your combo list , turning the Clearbeforfill property of your Table Adapter to False can prevent that.