Databound Radiobuttons

  • Thread starter news.microsoft.com
  • Start date
N

news.microsoft.com

Hello,

I have two databound radiobuttons which have advanced databinding properties
of onpropertychanged enabled so that I can raise the columnchanged event
whenever the radiobuttons are changed.

In the columnchanged event I am simply accepting changes into the dataset.
There is no dataadapter because it is a standalone dataset.

When one radiobutton is already checked (as opposed to neither) and I select
the non-checked radiobutton, the columnchanged event is raised twice and
erases the checked value of both radiobuttons in the acceptchanges call. It
is raised twice because it is first raised to un-check the first radio
button, and then called again to check the second radio button... but on the
second call it somehow sees that the second radio button is not physically
checked yet, so sends a false value.

This is pretty simple to reproduce and funnily enough nothing seems to be
available on the net to explain this with radiobuttons.


Troy
 
S

Simmo

Probably to explain a little better:

You have Radiobutton #1 and Radiobutton #2.

If #1 is checked and you click #2, firstly the two radiobuttons physically
swap the checked status and are visibly correct. Then the columnchanged
event is raised for the dataset. The event is firstly handled for #1 and
acceptchanges is called... this causes the new false value for #1 to be
saved, *and* for the existing false value for #2 (still false on the
datarow), triggering the stored value and the checked value for #2 to be
changed to false. This triggers the columnchanged event again and simply
places false values into the radiobutton databound columns with
acceptchanges.

Is there any way to break that cycle?


Troy
 
S

Simmo

Thanks Ken, just curious if there is a known workaround without using a
custom control? I've got separate columns in the dataset for each
radiobutton... rather than the scenario of one column and separate values
for each radiobutton; it's a matter of semantics really, especially when
only dealing with only a couple of paired groups of radiobuttons.

I wanted to try and keep the solution simple and have no control-initiated
events to update the dataset outside of the databindings setting. I have
seen the custom control solution before, but I'm thinking that there has to
be a far simpler answer.

I think the biggest hurdle is getting around the dataset's acceptchanges
overriding the newly checked radiobutton with the old dataset contents,
sparking the change to be lost.


Troy
 
S

Simmo

I figured out a simple hack. At this stage I think it will only work with a
two radiobutton group.

Here's the hack:

Create the two radiobutton group and databind them to two separate table
values. Setup the databindings checked event for each radiobutton, and
under advanced, change the event type to onpropertychanged. If you run the
project now, you will see the problem reproduced, where it actually takes
two clicks to change to a new radiobutton value. To fix this, in ONE of the
radiobutton column definitions, change the expression to 'Not
otherradiocolumn', where otherradiocolumn is the name of the table column
for the OTHER radiobutton. This will ensure that the radiobuttons always
switch and never cancel one another out.

If this solution could be expanded to three or more radiobuttons it would
remove the need for messy usercontrols, though I'm not sure it would be
possible.


Troy
 

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