Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple way to bind radiobuttons to SQL table in C#

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?

like image 272
G Berdal Avatar asked Dec 29 '22 20:12

G Berdal


2 Answers

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.

like image 191
lc. Avatar answered Jan 05 '23 08:01

lc.


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.

like image 24
tekBlues Avatar answered Jan 05 '23 08:01

tekBlues