Load the combobox

Asked

Viewed 162 times

2

When save a dice in the combo box, and in this combo box has two options (gasoline, diesel, alcohol, natural gas)

When I select diesel and save it works normally.

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

How do I stop 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";
}

1 answer

2


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

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

Example of use:

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);
}
  • 1

    Hi Tiago, blza? First of all thank you very much for the help, sanou 70%, but I have one more question, this Lerdafontededados() would be the list of items?

  • This Pathfinder method() If the data source is the list How would you implement this method?

  • Got it, solved the problem, thank you very much

  • The list is a data source of options the way you did it right, but now you need a user data source. For example: you need to persist user data, so when it reopens your app it will read from this data source (can be file, database and etc...) and will show the user the selection of it.

  • Got it, solved the problem, thank you very much

Browser other questions tagged

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