niv Posted July 23, 2003 Posted July 23, 2003 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 Quote
karimgarza Posted July 24, 2003 Posted July 24, 2003 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 Quote You're either a one or a zero. Alive or dead.
annie-r Posted July 20, 2005 Posted July 20, 2005 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; Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.