Combobox.Items not being updated

2

I'm creating a combobox in runtime and trying to manually set a% of 'default'%, but I realized that even setting a value inside the combo manually is not selected. p>

I already checked if the data type beats (both are SelectedValue ).

Below the code where I create the combo:

ComboBox cmb = new ComboBox()
{
    DropDownStyle = ComboBoxStyle.DropDown,
    BackColor = Color.FromArgb(210, 211, 213),
    ForeColor = Color.Black,
    Location = new Point(5, y),
    Size = new Size(265, 10),
    Font = new Font("Verdana", 11f, FontStyle.Regular),
    FlatStyle = FlatStyle.Popup,
    AutoCompleteMode = AutoCompleteMode.SuggestAppend,
    AutoCompleteSource = AutoCompleteSource.ListItems,
    Tag = null
};

cmb.DataSource = null;
cmb.ValueMember = "Id";
cmb.DisplayMember = "Login";
cmb.DataSource = UsuarioDB.FindAll().Where(x => x.Id > 0).OrderBy(x => x.Login).ToList();
cmb.SelectedValue = (long)1; //Aqui estou setando manualmente o valor para testar

There are three things to punctuate:

  • Debugging the code I see that the long property is correct with all the records, but the DataSource property is empty;
  • Even with the property Items empty, the combo shows the values exactly as I need (ie, running) and when changing the selected value and trying to capture by Items the value comes right.
  • In the same form a combobox is created (this in design time ), where I also set the SelectedValue manually, however this works fine.
  • Any idea what it might be?

        
    asked by anonymous 01.07.2015 / 19:43

    1 answer

    0

    Try this:

    cmb.SelectedIndex = 1;
    

    Or if you want to fill in from a value:

    int index = cmb.FindString("valor");
    cmb.SelectedIndex = index;
    
        
    01.07.2015 / 19:48