Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > databinding combobox AFTER combobox has been displayed, has no effect
 

databinding combobox AFTER combobox has been displayed, has no effect

If I do the combobox databinding during form initialization time (c'tor), things work OK, I see the column I specified in the DisplayMember, and the values are the expected ones. 
However, I don't always know which table, or even database a user will select at run time, so I want to populate the combobox after it has been displayed on the form empty. Well, databinding AFTER it is shown, does nothing. No data are shown.  Same exact code that works during form c'tor time does nothing after the form has been displayed. Spellings etc are all the same, as all is done using the same function.
I use a small utility function that creates and populates a dataset and binds it to the combobox. 

Now for the interesting part...

If I call this utility once (after combo has been displayed) no data are loaded into the combobox (as mentioned). Then if I call it again...voila! the data are there. Seems some event is not broadcasted when the datasource prop changes from null to something, but it is generated when the datasource is changed to a new object. Obviously in real life I do not want to have to create and populate big datasets twice, just to get around this #$@! combo problem. Any other solutions will be greatly appreciated. I tried binding to a 'fake' empty dataset but then I get the stupid DataViewRow...garbage item in the combobox since the fake empty dataset has no table to bind the DisplayMember to...duh!

TIA!

- erika

ps
I'm using WinForms and C#
MigrationUser 1  Wednesday, July 28, 2004 5:36 PM
Ok, for one, you just set the combo like this

combbox.datasource = datatable
combobox.displaymember = "ColumnDisplayName"
combobox.valuemember = "ColumValueMember"

Now don't use a dataset as a datasource, use the datatable in a dataset.

set it to nothing then to another datatable when you are changing it in runtime.

Also remember if the colum is as so "MyName" then don't set the display/value member as "myname", it's case sensitive and will end up showing you datarowview.

Hope this helps.
MigrationUser 1  Wednesday, July 28, 2004 11:37 PM
well...I have to use the dataset, as other stuff on the form databind to the dataset and I want to all be synchronized, so I have to use the same datasource. Furthermore, as I said, it's not that I don't know how to data bind a combobox. The code I wrote works during form initilization time...but it does not work after the combobox has been displayed on the screen. All other controls databind fine after having been displayed, including datagrids and lots of edit boxes. It's the combo box that has some problem. And the spelling of the columns is correct, caps where caps should be. Databinding to the combobox works if I set the datasource twice, it works if I set the datasource during init time, it does not work if I set it once after init time. I would like to know what I can do to notify the combobox that it's datasource has been changed so it can re-populate itself. There must be some mechanism like that because if I set the datasource twice, using different datasets, I do succeed to bind it to the 2nd datasource. 

Thanks,

- erika
MigrationUser 1  Thursday, July 29, 2004 1:28 PM
If you just want to repopulate it from the same source, then just fill the same datatable again. Or clear it and fill. Filling again will refresh the contents and add new ones that might have been added as long as you don't change the query and or struture. Clearing it and then filling will have the same effect, and you get to change the query.
MigrationUser 1  Thursday, July 29, 2004 2:59 PM

You can use google to search for other answers

Custom Search

More Threads

• The data Grid Focus problem..
• System.ArgumentException: This row already belongs to another table.
• Restrict Maximum No of Input in datagridview control's Cell during editing
• Show table data - Object reference not set to an instance of an object
• Add New row to DataTable doesn't refresh DataGridView control. Please Help!
• Treeview
• Class with able to Sorting and Binding
• Multiple database tables in a single Datagridview
• How to merge column in DataGridView ?
• How to get modified rows from a datagrid , when the changes are made on the editable datagrid itself