CollectionBase

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi, i have a windows application that use a listbox, and a class that derives
from collection base.

I use this code to fill the listbox, and util here no problem. The available
applications object its a collectionbase type that has objects of type
application_, and its filled from database...

this.ListBoxApps.DataSource=this.AvailableApplications;
this.ListBoxApps.ValueMember="ID";
this.ListBoxApps.DisplayMember="Name";

for other hand i have a button in the same form that when i click on it
deletes items from the collection, and of course from database, and then i
would like to refresh the listbox with the collection updated...

My problem comes when i try to delete the last item of the listbox, first i
delete from the collection the object that has the same index than the
selected index in the listbox, then i delete from database, and then i
refresh the listbox with the collection modified, all its fine, except when i
select after all of this another item in the listbox. The error that its
showed is:
--
Specified argument was out of the range of valid values. Parameter
name:Index was out of range. Must be non-negative and less than the size of
the collection.

This is the code i use in the onclick method of the button to delete:

Onclick
if(ListBoxApps.SelectedIndex!=-1)
{
//Get the application object from the application collection
Application_
applicationtodel=this.AvailableApplications.Get_(this.ListBoxApps.SelectedIndex);

//delete from database and collection
this.AvailableApplications.DelApplication(applicationtodel);
//Refresh the listbox.
this.ListBoxApps.DataSource=null;
this.ListBoxApps.Items.Clear();
this.ListBoxApps.DataSource=this.AvailableApplications;
this.ListBoxApps.ValueMember="ID";
this.ListBoxApps.DisplayMember="Name";
}
Any help would be grateful...

Thanks a lot
Regards.
Josema
 
After recreating your collection with values from the database you can just
rebind the list box...

ListBoxApps.DataSource = this.AvailableApplications; // the newly created
collection
ListBoxApps.DataBind();

I hope this helps.
 
Back
Top