Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

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/default.asp?url=/library/en-us/cpref/html/frlrfSystemWindowsFormsListBoxClassItemsTopic.asp?frame=true

You're either a one or a zero. Alive or dead.
  • 1 year later...
Posted
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 control's 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;

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...