Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here are two possible solutions.</p> <hr> <p><strong>Binding Format and Parse events</strong></p> <p>The <code>Binding</code> class has a built-in facility for on-the-fly transformations of bound data in the form of the <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.binding.format.aspx" rel="nofollow noreferrer">Format</a> and <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.binding.parse.aspx" rel="nofollow noreferrer">Parse</a> events.</p> <p>Here's how you would use those events with just the "Male" radiobutton. Create the binding in code, not in the designer:</p> <pre><code>// create binding between "Sex" property and RadioButton.Checked property var maleBinding = new Binding("Checked", bindingSource1, "Sex"); // when Formatting (reading from datasource), return true for M, else false maleBinding.Format += (s, args) =&gt; args.Value = ((string)args.Value) == "M"; // when Parsing (writing to datasource), return "M" for true, else "F" maleBinding.Parse += (s, args) =&gt; args.Value = (bool)args.Value ? "M" : "F"; // add the binding maleRb.DataBindings.Add(maleBinding); // you don't need to bind the Female radiobutton, just make it do the opposite // of Male by handling the CheckedChanged event on Male: maleRb.CheckedChanged += (s, args) =&gt; femaleRb.Checked = !maleRb.Checked; </code></pre> <hr> <p><strong>Computed Property</strong></p> <p>Another approach is to add a computed property to your datasource:</p> <pre><code>public bool IsMale { get { return Sex == "M"; } set { if (value) Sex = "M"; else Sex = "F"; } } </code></pre> <p>Now you can simply bind the Male radiobutton to this property on your datasource (just don't show this property in the grid).</p> <p>And again you can hook up Female to Male like so:</p> <pre><code>maleRb.CheckedChanged += (s, args) =&gt; femaleRb.Checked = !maleRb.Checked; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload