Removing items from a Combobox

Asked

Viewed 648 times

1

I have a ComboBox Status searching approximately 13 status of a table:

cboStatus.DisplayMember = "nome";
cboStatus.ValueMember = "codigo";
cboStatus.DataSource = CartoesVTBLL.Status();
cboStatus.SelectedValue = "";

In this table I have the field Restriction who owns the code such status. However, this Restriction is to show, ie when the ComboBox is in the code To and in the table has Constraint "O;C;D" I will show only to change the status of O,C and D. Basically I will remove anything other than this status, I know I will use the split to separate the codes but as I can get in this rule, maybe a

remove.items.where(x => x.codigo != status.codigo)

3 answers

1

How are you assigning a DataSource to the ComboBox, cannot handle your collection of items manually.

In your case, just do as you imagined:

cboStatus.DataSource = CartoesVTBLL.Status().Where(x => x.codigo != status.codigo);
  • Is that would be a point I wanted to reach, unfortunately did not work very well but thanks anyway!

  • Why didn’t it work?

0

I managed to get the result I wanted, but I think there should be a much more simplified way, in any case thanks to the 2 for the reply.

 string[] Array_Restricao = new string[13];               

                List<Cartao_Status> Lista_Status = new List<Cartao_Status>();               
                List<Cartao_Status> Lista_Att = new List<Cartao_Status>();                

                Lista_Status = CartoesVTBLL.Status("", "A");
                string Restricao = Lista_Status.First(x => x.Codigo == cartaoDTO.Status.Codigo).Restricao;

                Array_Restricao = Restricao.Substring(0, Restricao.Length - 1).Split(';');

                foreach (var res in Array_Restricao)
                {
                    Lista_Att.Add(Lista_Status.First(x => x.Codigo == res));
                }
                Lista_Att.Add(Lista_Status.First(x => x.Codigo == cartaoDTO.Status.Codigo));
                cboStatus.DataSource = Lista_Att;

0

You could add these values within a list.

List<String> lista = new List<String>();

And remove unwanted value

var itemToRemove = resultlist.Single(r => r.item == "O"); // ou os valores que deseja
lista.Remove(itemToRemove);

After that you clean your Combobox

cmbBox.Items.Clear();

And add inside the combobox the values left in the list.

  • A little confused, but thanks anyway!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.