Live Chat Icon For mobile
Live Chat Icon

I am populating a ListBox by binding it to an ArrayList. The initial display is OK, but later changes to my ArrayList are not shown in my ListBox. How can I get the changes to show properly

Platform: WinForms| Category: Data Binding

In an ArrayList, the ’plumbing’ is not available to support two-way binding as with a dataset. So, you have to handle the synchronization yourself. One way to do this is to set the listBox1.DataSource to null and then reset it to your ArrayList. Another way is to use the CurrencyManager as shown in the code below.

	private System.Windows.Forms.ListBox listBox1;
	private System.Windows.Forms.Button button1;
	private ArrayList myArrayList;

	public Form1()
		// Required for Windows Form Designer support

		myArrayList = new ArrayList();

		listBox1.DataSource = myArrayList;
	//change the arraylist
	private void button1_Click(object sender, System.EventArgs e)
		myArrayList[1] = 'pink';

		//use currency manger to sync up the listbox
		BindingManagerBase bm = this.listBox1.BindingContext[myArrayList];
		CurrencyManager cm = (CurrencyManager) bm;
		if (cm != null)
		//Or, you can just reset the datasource
		//listBox1.DataSource = null;
		//listBox1.DataSource = myArrayList;

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.