niv said:
Hi,
I have a Listbox on my form and I have set its Datasource
to a DataView.
When I try to add to this listbox I receive the following
error: " Cannot modify the items collection when the
Datasource property is set."
Anyone know how I can solve this?
Thanks,
niv
Use: myControl.DataSource = null
Add Items, then set DisplayMember and DataSource.
I use an ArrayList to populate a combobox.
If you are adding items to a control that was already bound (already had items), you need myControl.DataSource = null before adding more items to the control. That statement allows you to add new items to the ArrayList and re-bind your control to the collection. Set DisplayMember and ValueMember before setting DataSource.
In the following example I use an ArrayList instead of DataView.
(1) Clear the controls DataSource reference
//cbxMediaName is a ComboBox
cbxMediaName.DataSource = null;
(2) Add items to the ArrayList that is being used to bind to the list control.
int iItemNr = arrMedia.Count;
arrMedia.Add (new AddValue (strNewItem, iItemNr));
*Note: AddValue is a class I use for setting Display and Value, which expects a string and an integer. arrMedia is an ArrayList.
(3) Bind to the control; populate the list
cbxMediaName.DisplayMember= "Display";
cbxMediaName.ValueMember = "Value";
cbxMediaName.DataSource = arrMedia;