Multiple ComboBox DataSource BindingSource issue



..NET 2.0:

I put two combo boxes in a Winform, and a DataGridView.

Create a DataSource using the Data Source creation wizard against a
Business class object, which creates a BindingSource object.

Assign the DataSource of all three controls to the same BindingSource
Object and:
1) Change the SelectedIndex on one combobox changes for the other, and
selects a diff. row in the DataGridView
2) Change the row in teh datagridView and the Selected Index in the 2
combo boxes change.

I know this is by design, but is there a way to avoid this using a
single BindingSource object?

I thought that by putting one of the comboboxes in a GroupBox and
explicitly setting the BindingContext to a new Binding Context this
would keep that comboBox from changing indexes, but that doesn't have
any effect...

Basically what I'm trying to accomplish:
- Be able to assign the same list to multiple controls
- When an item is ADDED or REMOVED from the list, the controls
automatically reflect the new list
- When an item whose DisplayMember is used in the list is changed, it
too reflects in the controls
- When a SelectedIndex changes for one control it DOES NOT change in
the others (unless I explicity set it).

In other words, do everything the BindingSource object accomplishes,
but without the SelectedIndex change reflecting in all controls.


The BindingSource.Position will be shared by all controls bound to it,
so I do not think you will be able to get the 2 comboboxes and
datagridview to not share this current position if they are bound to
the same BindingSource.

You could create a BindingList<> object to hold your objects and then
bind that list to the three controls. Then setting the BindingContexts
to different objects would let the 3 controls have different positions
with a single list.

public partial class Form1 : Form
public Form1()

private void Form1_Load(object sender, EventArgs e)
BindingList<MyObject> list = new BindingList<MyObject>();
list.Add(new MyObject("Ramya", 43));
list.Add(new MyObject("Manju", 43));
list.Add(new MyObject("Gulnus", 43));
list.Add(new MyObject("Sona", 43));

dataGridView1.DataSource = list;
comboBox1.DataSource = list;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Name";

comboBox2.DataSource = list;
comboBox2.DisplayMember = "Name";
comboBox2.ValueMember = "Name";

comboBox1.BindingContext = new BindingContext();
comboBox2.BindingContext = new BindingContext();
dataGridView1.BindingContext = new BindingContext();


public class MyObject
private string mName;
private int mAge;

public MyObject(string s, int i)
mName = s;
mAge = i;
public string Name
get { return mName; }
set { mName = value; }
public int Age
get { return mAge; }
set { mAge = value; }
public override string ToString()
return Name;
Clay Burch
Syncfusion, Inc.

Eric Robishaw


One of the features I like about the BindingSource object is its ability to
update the controls when a property of the bound object that is used for the
DisplayMember changes w/o having to do anything in the object itself.

Other than implementing INotifyPropertyChanged in the object, can you think
of a way to use the BindingList for the datasource and have the item's
update reflect in the controls automatically?

I havn't taken the time to fully dig into the BindingSource object via
Reflector to see how it does its magic... I suppose I could attempt to
create my own BindingListEx which add's this feature from the
BindingSource... any ideas?

Thanks for your response.



One of the features I like about the BindingSource object is its ability to
update the controls when a property of the bound object that is used
for the
DisplayMember changes w/o having to do anything in the object itself.

I played around with the code above after adding a button where I
changed the Name property on a particular MyObject in the list. I may
be missing something, but I really could not see a difference in the
behavior between BindingSource and BindingList<> with respect to the
change in Name. If MyObject implemented INotifyPropertyChanged, then
all three controls reflected the change immediately. If MyObject did
not implement that interface, then the grid would only show the change
if and forced it to redraw and the combo dropdown list did not ever
show the change (though the textbox did show the change if you made
the changed item the current item in the grid).

Now instead of just changing a property on the object, you changed the
object itself, then in both types of DataSources, the change was
reflected immediately.

Clay Burch
Syncfusion, Inc.

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