Binding to combo and checkbox not synching

F

Flomo Togba Kwele

I have some comboxes and checkboxes on a UserControl, all of which are
bound to a BindingSource.

When I bound the bindingsource to the checkbox Checked property and
the user accepted the default (unchecked, false), the bindingsource
blew in the EndEdit event of the bindingsource on a null value for the
column in the dataset associated with the checkbox.

If I check the box and uncheck it before trying to save the row, the
error does not occur.

Similarly with the combos, if I select an item in the combobox at Load
time, it too gets the null error in the EndEdit event. The
bindingsource is bound to the combo's SelectedValue property. The
datasource of the combos are generic Lists of Strings, so I didn't
know what to place in the ValueMember's property.

I was forced to set the comboboxes SelectedIndex to -1 and bind the
checkboxes to the CheckedState property to force the user to change
state before saving.

There must be a better way to do this. Any suggestions?

Thanks
 
L

Linda Liu [MSFT]

Hi Flomo,

As for your first question:

Based on my understanding, you have a BindingSource component which has
been bound to a DataSet object. You bind the Checked property of a CheckBox
control to a field in the BindingSource component. When the user adds a new
row in the BindingSource and accept the default value (unchecked, false),
the value of the field in the BindingSource component remain null. If I'm
off base, please feel free to let me know.

It seems that the new row the user adds in the BindingSource has a null
value for the field. When the data source has either a value of Null or
False, the CheckBox control bound to the field has the Checked property of
the value of False. If the user doesn't change the CheckBox control's
checked state, data binding won't push the new value to the data source.
This behavior is by design.

To work around this problem, I suggest that you set the default value for
the field in the DataSet. To do this, open the DataSet in the designer by
double-clicking it in the Solution Explorer, and select the column in the
DataTable you want and set the column's DefaultValue property in the
Properties window.

As for your second question:

I performed a test based on your description. I set a ComboBox control's
the DataSource property to a List of strings(I didn't set its ValueMember
or DisplayMember), and bind its SelectedValue property to an other data
source. When the program is run, an exception occurs--'Cannot set the
SelectedValue in a ListControl with an empty ValueMember'.

In fact, in this scenario, we could bind the Text property of the ComboBox
control to the data source instead.

As for the problem of getting correct value in the data source after you
call the EndEdit method of the BindingSource component, ensure to set the
SelectedIndex/SelectedItem of the ComboBox AFTER it is bound to a data
source.

Hope this helps.
If you have anything unclear, please feel free to let me know.



Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
L

Linda Liu [MSFT]

Hi Flomo,

How about the problem now?

If the problem is still not solved and you need our further assistance,
please feel free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!


Sincerely,
Linda Liu
Microsoft Online Community Support
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top