2

I have notice that when two combobox share the same datasource( for example a DataTable),they will share the same selected index.

I would like to know how this behaviour is implemented.

It feels like that DataTable has fire some sort of event when a 'current row' or 'active row' have change, and the comboboxs that bind to it capture the event and make coresponding changes. However, I saw that event when you use datasource like List, it works too.

Actually I would like to make a RadioButton to update synchronously with the two combobox, how should I implement that?

Questions I saw on the web mainly deal with how to de-synchronize the combobox with same datasource,like this Multiple Combo Boxes With The Same Data Source (C#), and this Binding two combo boxes to the same data source,that each combo will have individual behaviour, Share ComboBox DataSource.

Community
  • 1
  • 1
Allan Ruin
  • 5,229
  • 7
  • 37
  • 42
  • Looking at [this post](http://stackoverflow.com/questions/23886653/comboboxes-are-linked-for-some-reason) I guess you would also use a common BindingSource (if that can indeed be bound to a RadioButton?!) – TaW Mar 14 '15 at 09:05
  • @TaW I know how to de-couple the common datasource problem. Here I just want to know how coupling of two comboboxs happend, so I could take advantage of the machanic. No, bindingsource cannot bind to radiobutton. – Allan Ruin Mar 14 '15 at 09:16
  • I know you knew, I just want to point out how the coupling is done in the Comboboxes. If you can't do it the same way for the RadioButton, I guess you will have to find some event driven method.. [`Bindingsource`](https://msdn.microsoft.com/de-de/library/system.windows.forms.bindingsource%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396) has a `PositionChanged` event.. – TaW Mar 14 '15 at 09:23

2 Answers2

1

This behavior is implemented by the BindingContext Class. The crucial part is:

For each data source on a Windows Form, there is a single CurrencyManager or PropertyManager.

If you set a ComboBox's (or ListControl's) datasource, it registers itself to the containing Control's BindingContext so it will communicate with its CurrencyManager. When two ListControls register with the same data source, they receive the same CurrencyManager.

You can simply check this by

this.comboBox1.BindingContext[lst].CurrentChanged += Form1_CurrentChanged;
this.comboBox2.BindingContext[lst].CurrentChanged += Form1_CurrentChanged;
this.BindingContext[lst].CurrentChanged += Form1_CurrentChanged;

void Form1_CurrentChanged(object sender, EventArgs e)
{
    Debug.WriteLine(((CurrencyManager)sender).GetHashCode());
}

This will show three identical hashcodes in the output window if you change the selected item of one combo box.

This also shows why there is no "shortcut" to a RadioButton's value, because it doesn't communicate with the CurrencyManager (it's not a ListControl). You'll have to write code to sync its value with a combo box's current value.

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
0

Here is a way to do it:

  • Create a BindingSource which you can use as the common DataSource for both ComboBoxes. So they still are coupled, but to a normal BindingSource, not the hidden default one: myBS = new BindingSource(myDT, myDT.Columns[myColumn].ColumnName);

  • Add an event for changes in the Position: myBS .PositionChanged += myBS_PositionChanged;

  • Put code in the event to control the values of the RadioButton..

The logic in the event code is up to you; here is an example:

void myBS_PositionChanged(object sender, EventArgs e)
{
   radioButton1.Checked = myBS.Current.ToString() == "2";
}
TaW
  • 53,122
  • 8
  • 69
  • 111