panjo
panjo

Reputation: 3515

remove delete item from listbox list of items

I have listbox with collection of items. After deleting item from the listbox I want to remove that item from the listbox without reloading whole collection again, is this possible in winforms?

 private void btnDelete_Click(object sender, EventArgs e)
        {
            MyData sel = (MyData)listBox1.SelectedItem;
            if (...delete data)
            {
                listBox1.Items.Remove(listBox1.SelectedItem);
                MessageBox.Show("succ. deleted!");
            }
            else
            {
                MessageBox.Show("error!");
            }           
        }

I'm getting error

items collection cannot be modified when the datasource property is set

Upvotes: 0

Views: 336

Answers (2)

Nick Hill
Nick Hill

Reputation: 4917

You should use an observable collection as a DataSource. You can use built-in ones, such as BindingList<T> and ObservableCollection<T>.

But you may also consider creating your own collection and implement either IBindingList or INotifyCollectionChanged interface.

Update

public partial class YourForm : Form
{
    private BindingList<string> m_bindingList = new BindingList<string>();

    private YourForm()
    {
        InitializeComponent();
        yourListBox.DataSource = m_bindingList;

        // Now you can add/remove items to/from m_bindingList
        // and these changes will be reflected in yourListBox.

        // But you shouldn't (and can't) modify yourListBox.Items
        // as long as DataSource is set.
    }

    private void btnDelete_Click(object sender, EventArgs e)
    {
        // Removing items by indices is preferable because this avoids
        // having to lookup the item by its value.
        m_bindingList.RemoveAt(yourListBox.SelectedIndex);
    }
}

Upvotes: 0

Neeraj
Neeraj

Reputation: 4489

Hey Try to Get Selected item index from your collection then remove item form collection by index then again bind your list box to collection..

I have made sample code please refer.

public partial class Form1 : Form
{
    List<String> lstProduct = new List<String>();
    public Form1()
    {

        InitializeComponent();
    }

    public List<String> BindList()
    {

        lstProduct.Add("Name");
        lstProduct.Add("Name1");
        lstProduct.Add("Name2");
        lstProduct.Add("Nam3");
        lstProduct.Add("Name4");

        return lstProduct;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        listBox1.DataSource = BindList();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        // The Remove button was clicked.
        int selectedIndex = listBox1.SelectedIndex;

        try
        {
            // Remove the item in the List.
            lstProduct.RemoveAt(selectedIndex);
        }
        catch
        {
        }

        listBox1.DataSource = null;
        listBox1.DataSource = lstProduct;
    }
}

Hope it helps you....

Upvotes: 1

Related Questions