Add item to a listbox that is bind to a DataSource
-
I have a simple list box that contains some string values. This list box is initially populated by binding to a collection (via DataSource field). However later user will be able to add some values into it also. When I just do a listbox.Items.Add(mystring), this new values never show up in the listbox itself. Any ideas what I am doing wrong? Or may be there are standard patterns for dealing with this type of situation?
-
I have a simple list box that contains some string values. This list box is initially populated by binding to a collection (via DataSource field). However later user will be able to add some values into it also. When I just do a listbox.Items.Add(mystring), this new values never show up in the listbox itself. Any ideas what I am doing wrong? Or may be there are standard patterns for dealing with this type of situation?
When bindigng to a collenction, you have to add the item to the collection not the listbox.Items. Watch out for Exceptions!!!
listBox1.DataSource = yourListOrCollection;
//to add an item, unbound the listBox
listBox1.DataSource=null;//add the item to the collection
yourListOrCollection.Add("Something");
//and set the updated collection as the data sourcelistBox.DataSource = yourListOrCollection;
-
When bindigng to a collenction, you have to add the item to the collection not the listbox.Items. Watch out for Exceptions!!!
listBox1.DataSource = yourListOrCollection;
//to add an item, unbound the listBox
listBox1.DataSource=null;//add the item to the collection
yourListOrCollection.Add("Something");
//and set the updated collection as the data sourcelistBox.DataSource = yourListOrCollection;
Thanks for the reply. Problem is that my collection is actually an entity collection that is returning bunch of columns from table and I am just using one of the column to populate this listbox. I therefore not sure if I can add this item into the collection.
-
Thanks for the reply. Problem is that my collection is actually an entity collection that is returning bunch of columns from table and I am just using one of the column to populate this listbox. I therefore not sure if I can add this item into the collection.
sure there is. nothing is impossible. You could trick/force something like that in many ways. 0) Add a whole item to the collection with default/empty values for the not used cols(delete them after words) 1) Createa a separate array or list of strings from that column ...
-
sure there is. nothing is impossible. You could trick/force something like that in many ways. 0) Add a whole item to the collection with default/empty values for the not used cols(delete them after words) 1) Createa a separate array or list of strings from that column ...