pawel
pawel

Reputation: 6136

How to delete object from combobox?

I have a combobox with objects of Foo type, here is the Foo class:

public class Foo
{
    public string name { get; set; }
    public string path { get; set; }
}

The Foo.name is the displayed text in the combobox and Foo.path is the value of the selected option.

I want to delete an option from the combobox after some operation I made.

I've tried these ways:

Nothing works for me. : / How to do this?

UPDATE

This is how I'm initializing my combobox:

    string[] filePaths = Directory.GetFiles(sites.paths[comboBox1.SelectedIndex]);

        List<Foo> combo2data = new List<Foo>();

        foreach (string s in filePaths)
        {
            Foo fileInsert = new Foo();
            fileInsert.path = s;
            fileInsert.name = Path.GetFileName(s);
            combo2data.Add(fileInsert);
        }

        comboBox2.DataSource = combo2data;
        comboBox2.ValueMember = "path";
        comboBox2.DisplayMember = "name";

Upvotes: 2

Views: 28415

Answers (7)

Leandre_B
Leandre_B

Reputation: 1

Perhaps delete all items in the Combobox with comboBox.Items.Clear();

Upvotes: 0

Daniel Melo
Daniel Melo

Reputation: 171

I think the secret is to first attribute null to the datasource and after rebind to a modified collection:

int idToRemove = 1;
var items = (cbx.DataSource as List<MyEntity>);
items.RemoveAll(v => v.Id == idToRemove);
rebindCombobox(cbx, items, "Name", "Id");


private void rebindCombobox(ComboBox cbx, IEnumerable<Object> items, String displayMember, String valueMember)
{
    cbx.DataSource = null;
    cbx.DisplayMember = displayMember;
    cbx.ValueMember = valueMember;
    cbx.DataSource = items;
}

Upvotes: 0

Stig
Stig

Reputation: 1323

These 2 commands will remove an item from your data source.

list.Remove((Foo)comboBox1.SelectedItem);

or

list.Remove(list.Find(P=>P.name == comboBox1.SelectedText));

Upvotes: 0

Arif Eqbal
Arif Eqbal

Reputation: 3138

Suppose you want to Remove Items by Index:

    combo2data.RemoveAt(0); //Removing by Index from the dataSource which is a List

    //Rebind
    comboBox2.DataSource = null;
    comboBox2.DataSource = combo2data;  
    comboBox2.ValueMember = "path";  
    comboBox2.DisplayMember = "name";  

Suppose you want to Remove by seraching for a member value

    Foo item = combo2data.Where(f => f.name.Equals("Tom")).FirstOrDefault();
    if (item != null)
    {
        combo2data.Remove(item);
        comboBox2.DataSource = null;
        comboBox2.DataSource = combo2data;  
        comboBox2.ValueMember = "path";  
        comboBox2.DisplayMember = "name";  
    }

Upvotes: 0

Ebad Masood
Ebad Masood

Reputation: 2379

comboBox2.Items.Remove(comboBox2.SelectedValue); will only remove from the combobox, not from the datasource bound to the combobox. You may remove it from the datasource and re-bind the datasource.

Upvotes: 5

HatSoft
HatSoft

Reputation: 11201

combox1.Remove(takes an object)
Object selectedItem = comboBox1.SelectedItem;

So you cna do it this way combox1.Remove(selectedItem);

Upvotes: 0

user586399
user586399

Reputation:

Use ComboBox.SelectedIndex property.

For example: let me have comboBox1 added to the form. In the delete button:

if (comboBox1.SelectedIndex >= 0)
    comboBox1.Items.RemoveAt(comboBox1.SelectedIndex);

Upvotes: 1

Related Questions