Dinesh Reddy Alla
Dinesh Reddy Alla

Reputation: 1717

How to delete item from List Object in Windows Forms?

I am Having a employees List in List object(objListEmp) as shown below.

Dinesh    
Upendra
Chandrakanth(Deleted)

My question is now i want to display employees in Combox in windows application which are in active mode i.e.

Dinesh    
UPendra

my code for binding data in to ComboBox is

if (objListEmp != null)
{
    cmbAssignedSelector.DataSource = new BindingSource(objListEmp, null);
    cmbAssignedSelector.DisplayMember = "Value";
    cmbAssignedSelector.ValueMember = "Key";
}
else
{
    cmbAssignedSelector.SelectedIndex = 0;
}

I Tried As shown below

if (objListEmp != null)
{
    cmbAssignedSelector.DataSource = new BindingSource(objListEmp.Where(x=>x.Value.Split('(').ToString()!="InActive").ToList(), null);
    cmbAssignedSelector.DisplayMember = "Value";
    cmbAssignedSelector.ValueMember = "Key";
}
else
{
    cmbAssignedSelector.SelectedIndex = 0;
}

Upvotes: 1

Views: 172

Answers (3)

Nitin Varpe
Nitin Varpe

Reputation: 10694

You can use where clause and mentions predicate which will only take employees which are active (Or you can have any other custom condition depending on your requirement.)

 objListEmp = objListEmp.Where(x=>!x.Inactive).ToList();

You can also compare string in that case.

 objListEmp = objListEmp.Where(x=>x.Status!=="InActive").ToList();

Upvotes: 2

Dinesh Reddy Alla
Dinesh Reddy Alla

Reputation: 1717

if (objListEmp != null)
{
    // To remove Deleted user from list
    var sorted = from employee in objListEmp
                 where !employee.Value.Contains("Deleted")
                 select employee;

    cmbAssignedSelector.DataSource = new BindingSource(sorted, null);
    cmbAssignedSelector.DisplayMember = "Value";
    cmbAssignedSelector.ValueMember = "Key";
}
else
{
    cmbAssignedSelector.SelectedIndex = 0;
}

Output:

Dinesh
Upendra

Upvotes: 0

Uthman Rahimi
Uthman Rahimi

Reputation: 708

change this line :

 cmbAssignedSelector.DataSource = new BindingSource(objListEmp, null);

to

cmbAssignedSelector.DataSource = new BindingSource(objListEmp.Where(x=>x.Isactive).ToList(), null);

If an IsActive Field is string and you save bool vale in filed like 'True' you need cast befor like this

  cmbAssignedSelector.DataSource = new BindingSource(objListEmp.Where(x=>(bool)x.Isactive).ToList(), null);

And if you save 'IsActive ' in a Field you just need compare with String like this :

objListEmp.Where(x=>x.Isactive.ToLower()="isactive").ToList()

Upvotes: 1

Related Questions