How to delete object from combobox?

前端 未结 6 1961
遇见更好的自我
遇见更好的自我 2021-01-25 18:37

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

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


        
6条回答
  •  时光取名叫无心
    2021-01-25 18:49

    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";  
        }
    

提交回复
热议问题