I'm trying to bind an SQL table to radio buttons in a Windows Forms application.
The buttons are grouped by group boxes. I haven't yet find a way to bind them correctly to the datasource. I know some had managed to do this by creating a custom control, but is there a simpler way?
Edit: This is probably a cleaner way to do it (untested, but should work).
You should actually bind directly (using a Binding
) to the radiobuttons, but handle the Binding.Parse
and Binding.Format
events.
In the 'Format' event, set the Checked properties and in the Parse
event, you'll save the value back based on which one is checked.
Original Idea: Without creating a custom control or subclassing, the simplest way is probably not to bind directly but to load and save the values yourself as an intermediary.
One way is:
Subscribe to the BindingSource.CurrentItemChanged
event and set the radiobutton Checked
properties accordingly.
Subscribe to the RadioButton.CheckedChanged
events and set the underlying data source appropriately.
I've used a dirty trick:
Create a hidden textbox. Bind it to the datasource, and leave the radio buttons unbound. Then program the textbox change event to set the radio buttons value and the radio button click events to set the text bot value.
Dirty, but it works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With