Datasource and Listbox Add issue

niv

Member
Joined
Jun 3, 2003
Messages
9
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
 
checkout this link that says

You can also manipulate the items of a ListBox by using the DataSource property. If you use the DataSource property to add items to a ListBox, you can view the items in the ListBox using the Items property but you cannot add or remove items from the list using the methods of the ListBox.ObjectCollection.

http://msdn.microsoft.com/library/d...owsFormsListBoxClassItemsTopic.asp?frame=true
 
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;
 
Back
Top