Matt
Matt

Reputation: 15061

Delete selected item from listbox with button from window form

I have a listbox and i want to select and item in it and press a button to delete that from the database. I can edit and save fine just not delete.

Current code:

private void button1_Click_3(object sender, EventArgs e)
{
     if (listBox1.Items.Count >= 1)
          {
               if (listBox1.SelectedValue != null)
               {
                    listBox1.Items.Remove(listBox1.SelectedItem);
                    System.Windows.Forms.MessageBox.Show("Item Deleted");
               }
          }
     else
     {
          System.Windows.Forms.MessageBox.Show("No ITEMS Found");
     }
}

I am getting the error:

Items collection cannot be modified when the DataSource property is set.

Upvotes: 2

Views: 3525

Answers (2)

vCillusion
vCillusion

Reputation: 1799

Assuming there can be multiple items selected on the ListBox. Below logic will remove from the UI. Also, you can add logic to remove the items from the database if required.

Below exception occurs when Data Bindings are different than data sources.

Items collection cannot be modified when the DataSource property is set

A data source specifies the source of data whereas data bindings define how data is bound to once it is retrieved. Typically, data binding occurs only during UI initialisation, and it doesn't change after that.
The data source itself can change over the life of control, but this is rare.

Here, the data source elements change.

On to your actual error. You cannot set DataSource and modify the Items property. It isn't allowed. The correct way to handle this is to add the items to your data source. This insertion in your data source will cause the control to update the list automatically. The ListBox Control relies on the DS to store the data so all changes must go through that. In the rare case where you need to be able to edit the Items directly then, you'll have to do away with DataSource and manually populate the Items collection. This update to DataSource usually is not needed though.

Below code to reset the data source

if(listBox1.SelectedItems != null)
{
    // Assuming its List of string
    var items = listBox1.DataSource as List<string>;  

    // Remove multiple selected items
    var count = listBox1.SelectedItems.Count;    
    while(count != 0)
    {
        var selectedItem = listBox1.SelectedItems[count-1];
        if(items.ContainsKey(selectedItem))
        {
            items.Remove(selectedItem);
        }
        count--;
    }
    listBox1.DataSource = null;
    listBox1.Items.Clear();
    listBox1.DataSource = items;
}

An optimised approach to store the data source and modify it to update the data source directly. Use the ObservableCollection type and update the collection to update the UI.

public class NamesClass : DependencyObject
{
   public ObservableCollection<string> Names {get; private set; }
   public TestClass()
   {
            this.Names = new ObservableCollection<string>();
   }
}

Now include the DependencyObject class as DataSource and update the Names collection.

Upvotes: 0

k1dev
k1dev

Reputation: 641

private void button1_Click_3(object sender, EventArgs e)
{
     if (listBox1.Items.Count >= 1)
          {
               if (listBox1.SelectedValue != null)
               {
                    var items = (List<YourType>)listBox1.DataSource;

                    var item = (YourType)listBox1.SelectedValue;
                    listBox1.DataSource = null;
                    listBox1.Items.Clear();
                    items.Remove(item);
                    listBox1.DataSource = items;
               }
          }
     else
     {
          System.Windows.Forms.MessageBox.Show("No ITEMS Found");
     }
}

This will work

Upvotes: 2

Related Questions