Load the combobox

2

When you save a data in the combo box, and in this combo box you have two options (gasoline, diesel, alcohol, natural gas)

Selecting diesel and saved works normally.

The problem is when I want to load the combo box save always the first value of the combo appears, ie the gas.

How do I do when the user loads the information he can see in the combo box the value selected by him?

The code is below

public frmCadVeiculo2()
{
    InitializeComponent();

    List<Combustivel> itens = new List<Combustivel>();
    itens.Add(new Combustivel() { IDCombustivel = "Ga", Nome = "Gasolina" });
    itens.Add(new Combustivel() { IDCombustivel = "Di", Nome = "Diesel" });
    itens.Add(new Combustivel() { IDCombustivel = "Al", Nome = "Álcool" });
    itens.Add(new Combustivel() { IDCombustivel = "GN", Nome = "Gás Natural" });

    cbTipoCombustivel.DataSource = itens;
    cbTipoCombustivel.ValueMember = "IDCombustivel";
    cbTipoCombustivel.DisplayMember = "Nome";
}
    
asked by anonymous 18.04.2017 / 17:23

1 answer

2

Implement the method below on your form and when you load the saved data, proceed to the method the selected fuel:

private void SelecionarTipoCombustivel(Combustivel combustivel)
{
    cbTipoCombustivel.SelectedValue = combustivel.IDCombustivel;
}

Example usage:

public frmCadVeiculo2()
{
    InitializeComponent();

    List<Combustivel> itens = new List<Combustivel>();
    itens.Add(new Combustivel() { IDCombustivel = "Ga", Nome = "Gasolina" });
    itens.Add(new Combustivel() { IDCombustivel = "Di", Nome = "Diesel" });
    itens.Add(new Combustivel() { IDCombustivel = "Al", Nome = "Álcool" });
    itens.Add(new Combustivel() { IDCombustivel = "GN", Nome = "Gás Natural" });

    cbTipoCombustivel.DataSource = itens;
    cbTipoCombustivel.ValueMember = "IDCombustivel";
    cbTipoCombustivel.DisplayMember = "Nome";

   //Aqui você faz a leitura da sua fonte de dados onde você salvou a seleção do usuário.
   Combustivel combustivelSelecionado=LerDaFonteDeDados();

   //Aqui faz a seleção do combobox;
   SelecionarTipoCombustivel(combustivelSelecionado);
}
    
18.04.2017 / 19:01