Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Items collection cannot be modified when the DataSource property is set." [closed]

Tags:

c#

Having this issue with a program that adds files through a form to a txt file but the issue doesn't say anything about Fstream so i'm thinking it doesn't have to deal with it but i'm not sure what this problem means.

lstEmployees.Items.Add("No records found.");
like image 893
David Brewer Avatar asked Feb 17 '11 22:02

David Brewer


2 Answers

The error message tells us that you have set the "DataSource property" on "lstEmployees". So go to "lstEmployees" properties and remove the DataSource - or if you want to keep the DataSource, don´t try to add "your own" items to "lstEmployees", as it won´t be accepted.

like image 65
Jack Johnstone Avatar answered Oct 05 '22 10:10

Jack Johnstone


Once you add a .DataSource to your listbox, you cannot modify the ListBox.Items collection. Instead, you can modify the original source.

For example, if your listbox is bound to a generic list of strings:

List<string> myList = new List<string>();
myList.Add("Item 1");
myList.Add("Item 2");
myList.Add("Item 3");

myListBox.DataSource = myList;

// need to add an item to the list after it's bound
myList.Add("No records found.");
like image 27
C-Pound Guru Avatar answered Oct 05 '22 10:10

C-Pound Guru