Link to home
Start Free TrialLog in
Avatar of kperelman
kperelman

asked on

vb.net combobox binding

I have a winform with a datagridview and a combobox.  In the form_load event I create a dataset table and then set it to the combobox's datasource.   When I move from datagridview row to row, the combobox.text stays in synch just like it should .. the selectedvalue field is in the same datasource as the datagridview's datasource.  The problem is when I change the datasource of the combobox, the combobox displaymembers and valuemembers change as they should per the coding, but the combobox does not automatically synch to the displaymember (based on the selectedvalue matching an matching valuemember in the combobox list).

So my questions are:
a) Why does the synching work in the form_load event automatically?
b) Why does changing the datsource cause the synching to not work and how do I fix the synching?
c) Can I reference the combobox's pre-datasource change 'selectedvalue' (but the combobox.text is null because the selectedvalue cannot find a matching valuemember thus the reason for needing to update the combobox datasource)
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

I could not really understand what you mean. Could you please explain what you are trying to do especially in terms of functionality and in terms of coding.
Avatar of kperelman
kperelman

ASKER

CodeCruiser:

Sorry about the ambiguity .. let me try again.  

Basically as I move from row to row in the DGV the combobox list of values needs to change (its dependent on the dgv row data).  

For example, I have a dgv with 2 columns (column1 and column2) and 3 rows.  The column1/column2  values in the rows are (1,11), (2,12) and (3,13).  The combobox selectedvalue is bound to field2 (the same field2 that the dgv uses .. same datasource).  

When I enter each dgv I need to update the combobox displaymember/valuemember list. So I load the dataset datatable with displaymember/valuemember pairs of (ItemC,13), (ItemB,12) and (ItemA,11) and update the combobox displaymember, valuemember and datasource.

I click into the dgv row 1 column1 cell.  The problem is the combobox valuemember of 11 is not binding to field2's selectedvalue of 11.  The combobox.text shows the first entry (ItemC) in the list which is not correct .. it should be ItemA.  If I manually correct the combobox selected value for row1 to ItemA and then go to row2, the combobox shows ItemB .. and then go to row3, the combobox shows ItemC .. all works again.

It's like I need to tell the combobox to refresh its bindings after I update the combobox displaymember/valuemember list?

Is this any clearer?
How do you bind the combobox? Did you use the property window to set the databindings?
CodeCruiser:

Yes I used the property window in the IDE when I created the combobox.  I did not touch it in the code.
What property you have bound and how? May be doing it in the code would help.
CodeCruiser::

I am not sure how to do this in code?

a) Should I use combobox.databindings.add()
b) If yes, should I do a combobox.databindings.remove() or a combobox.databindings.clear()
c) Where should I place (a) .. before or after the setting the combobox.datasource?
Did you say that you want the combobox to change when you click on a different row in the datagrid?
CodeCruiser:

Yes
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for your help.