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
//
InitializeComponent();
myArrayList = new ArrayList();
myArrayList.Add('orange');
myArrayList.Add('green');
myArrayList.Add('blue');
myArrayList.Add('red');
listBox1.DataSource = myArrayList;
}
......
//change the arraylist
private void button1_Click(object sender, System.EventArgs e)
{
myArrayList[1] = 'pink';
myArrayList.Add('lavendar');
//use currency manger to sync up the listbox
BindingManagerBase bm = this.listBox1.BindingContext[myArrayList];
CurrencyManager cm = (CurrencyManager) bm;
if (cm != null)
cm.Refresh();
//Or, you can just reset the datasource
//listBox1.DataSource = null;
//listBox1.DataSource = myArrayList;
}
Share with