How to delete object from combobox?

前端 未结 6 1997
遇见更好的自我
遇见更好的自我 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 19:00

    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);
    items.RemoveAll(v => v.Id == idToRemove);
    rebindCombobox(cbx, items, "Name", "Id");
    
    
    private void rebindCombobox(ComboBox cbx, IEnumerable items, String displayMember, String valueMember)
    {
        cbx.DataSource = null;
        cbx.DisplayMember = displayMember;
        cbx.ValueMember = valueMember;
        cbx.DataSource = items;
    }
    
        

    提交回复
    热议问题