lucgallant

I've got a question which I've been trying for a little while but just can't seem to get to the answer.

I have a combo box. As well, I have an ArrayList with my own objects in there. The object inside the array list is of class Employee, with a data member of type String inside named "name". Basically, what I want to do is bind the ArrayList and the combo box so that the combo box lists all the Employees in the ArrayList. I've got this:

drpdEmployee.DataSource = employeeList;
drpdEmployee.DisplayMember = "name";

This does work, except that after when I add items in the ArrayList, they don't get added to the combo box. However, if I step through the code, I see the new items in the DataSource. From this link:

http://msdn2.microsoft.com/en-us/library/system.windows.forms.combobox.datasource(vs.80).aspx

I did read that "When the DataSource property is set, the items collection cannot be modified.", but I see the DataSource containing my additional items, I just want them to also display in the combo box. Am I supposed to redo the DataSource

Any info would be very much appreciated, thanks,

Luc Gallant



Re: Windows Forms Data Controls and Databinding Question regarding DataBinding and Combo Boxes

Bora Inc

You have to rebind when ArrayList changed.

something like

drpdEmployee.DataBind();

but this will reset the selected value, so you need to re-set the existing value.

Regards,

Bora Inceler





Re: Windows Forms Data Controls and Databinding Question regarding DataBinding and Combo Boxes

Oleh Svintsitskyy

Hi Luc,

Use BindingList instead of Arraylist as it supports two-way data binding.

Regards,
Oleh.





Re: Windows Forms Data Controls and Databinding Question regarding DataBinding and Combo Boxes

lucgallant

Oleh,

thank you very much, that worked great! All my items now show up in the combo box!

Luc





Re: Windows Forms Data Controls and Databinding Question regarding DataBinding and Combo Boxes

lucgallant

Hello All,

the first part of my question was answered properly, and that worked just perfectly.

I'm having an issue now that I'm having a little more trouble getting my head around. I basically have the same scenario as last time:

----------------------------------------------------------------------------------------------------------------------------------

ComboBox drpdEmployee1 = new ComboBox();

ComboBox drpdEmployee2 = new ComboBox();

BindingList<employee> employeeList = new BindingList<Employee>();

...

...

drpdEmployee1.DataSource = employeeList;

drpdEmployee1.DisplayMember = "nameAndID";

drpdEmployee2.DataSource = employeeList;

drpdEmployee2.DisplayMember = "nameAndID";

----------------------------------------------------------------------------------------------------------------------------------

So both those Combo Boxes map properly to the list. Lets say there are three items in the list

a

b

c

Well, when I change the list selection from a to b, the list selection changes on all the combo boxes in that form. That's what is truly bothering me. In my scenario I have about 50 combo boxes and I need all of them to use the same list but not all have the same SelectedIndex.

Any suggestions would be greatly appreciated, Thanks,






Re: Windows Forms Data Controls and Databinding Question regarding DataBinding and Combo Boxes

Oleh Svintsitskyy

Hi Luc,

You are sharing the same BindingContext for each combo.

As a workaround this could be avoided by setting each combo clone of original collection(but do not clone colletion items)

Code Snippet

public Form1()

{

InitializeComponent();

BindingList< string > list = new BindingList< string >();

list.ListChanged += new ListChangedEventHandler(list_ListChanged);

list.Add( "a");

list.Add( "b");

list.Add( "c");

list.Add( "d");

}

void list_ListChanged(object sender, ListChangedEventArgs e)

{

BindingList< string > list = sender as BindingList< string >;

// update data source for each combo if some one's data source collection changed

foreach(Control control in this.Controls)

{

ComboBox combo = control as ComboBox;

if(combo != null)

{

BindingList< string > listCopy = new BindingList< string >();

// copy values

foreach(string obj in list)

{

listCopy.Add( obj );

}

// subscribe for list changes

listCopy.ListChanged += new ListChangedEventHandler(list_ListChanged);

// update data source

combo.DataSource = listCopy;

}

}

}

Simply change string to Your type.

Regards,
Oleh.





Re: Windows Forms Data Controls and Databinding Question regarding DataBinding and Combo Boxes

lucgallant

Oleh,

I can't thank you enough! I marked both the answers you gave me as correct answers, hopefully this helps your rating. I will post this thread on my webpage since I feel it may be helpful to others. Thanks a lot,

Luc