Combobox.Selectedindex does not return the item in Windows Forms

Asked

Viewed 1,082 times

0

I have a program in windows Forms and I need to select the item in a combo box so that the user can edit the record, however, it receives the value, finds the index but does not return the selected on the screen. follows the code:

private void PreencherCbox(List<Natureza> lista)
    {
        List<CboxModel> model = new List<CboxModel>();

        CboxModel p = new CboxModel();

        p.Text = "Selecione uma natureza";
        p.Value = "0";

        model.Add(p);

        foreach (Natureza n in lista)
        {
            CboxModel m = new CboxModel();
            m.Text = n.Descricao;
            m.Value = n.Id.ToString();

            model.Add(m);
        }

        this.CboxNatureza.DataSource = model;
        CboxNatureza.DisplayMember = "Text";
        CboxNatureza.ValueMember = "Value";
    }

and when to receive the value and select:

this.CboxNatureza.SelectedIndex = CboxNatureza.FindStringExact(e.Natureza.Descricao);

I have another piece of code that is the same, but it works. Help! = D

  • The correct would not be you refer to the value set for this.CboxNatureza.SelectedValue?

  • I tried too and could not. In my other form I do the same way as shown above and works perfectly...

1 answer

0

You are creating a model (CboxModel), just to add a simple text (Select a nature), you have tried using a Label to illustrate such a text?

Try it like this: inserir a descrição da imagem aqui

  • The estate .SelectedIndex receiving -1, leaves the text field empty.
  • Use the property .SelectedValue for the return of the item, note that the property .ValueMember was fed the ID field class Natureza which is in the List<>, so it goes returns a number.
  • The type of return of .SelectedValue is a object, then depending on the use mind the treatment.

Soon your code would look like this:

//-->Seu Método
private void PreencherCbox(List<Natureza> lista){
   this.CboxNatureza.DataSource = lista;
   CboxNatureza.DisplayMember = "Descricao";
   CboxNatureza.ValueMember = "Id";
   CboxNatureza.SelectedIndex = -1;       
}
  • Boy, I understand what you explained, but this form that I’m creating, it has a builder that gets an ID. After receiving this Id, I return the entire record to this form and here comes the need to already leave selected in the dropdown the corresponding nature of this record.. I don’t need a new event for this. I don’t know if I expressed myself right

  • @Ivojúnior from what you just said, I understand that you are developing a Form for editing something and loading it Form you receive an ID that defines the Item(qualquer coisa) which is being edited. This Item in this case already has Nature and you are not able to load it in the ComboBox. That’s it?

  • Correct! Everything is already ok, except the selection of nature in the combobox

  • Use the method the way it is now. Another thing, if you don’t solve it, show a print of what’s happening (how the combobox is reacting in Load()).

  • this.CboxNatureza.SelectedIndex = CboxNatureza.FindStringExact(en.Natureza.Descricao); This code returns index 2, which is the position of the item corresponding to that nature that should appear selected in the form, but in the view it continues to display the first dropdown item.

  • It really was supposed to be working because .FIndStringExact() returns a int which corresponds to the index. What follows after this assignment? If something comes, have you checked if any method is causing this change in .SelectedIndex of CboxNatureza ?

  • That’s the last line. There’s nothing else...

Show 2 more comments

Browser other questions tagged

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