How to put a value that is saved in the database in a combobox?

I am using C # Windows Forms with .NET 3.5.

I entered all Federation states within the items property of my combobox. When I search for a saved record from my DB, the combobox does not assume the value.

For example: when I look for a record that contains " AC " in the bank table, the combobox does not assume the value, it is blank.

The Code:

Conta conta = new Conta();
conta = controle.Ler(id);            

cboEstado.SelectedItem = conta.estado;            
Author: cvbarros, 2014-01-31

3 answers

To be simpler. Based on your Example:

Conta conta = new Conta();
conta = controle.Ler(id); 
cboEstado.DataSource = conta;
cboEstado.ValueMember = "ID ou IDENTIFICADOR UNICO DO REGISTRO";
cboEstado.DisplayMember = "SIGLA (acho que deve ser estado o nome da sua coluna)";
cboEstado.SelectedItem = conta.estado;
cboEstado.Refresh();
 1
Author: okevinlira, 2014-01-31 13:10:29

You need to assign the DataSource in your Combobox , the "states" class has the attributes " Id " and "acronym"

public class Estados
{
    public int Id { get; set; }
    public string Sigla { get; set; }

    public Estados(int id, string sigla)
    {
        this.Id = id;
        this.Sigla = sigla;
    }
}

In the form Load, we make the call:

private void Form1_Load(object sender, EventArgs e)
{
    List<Estados> estados = new List<Estados>(); /* Criei uma lista do tipo Estados */
    estados.Add(new Estados(1, "SP")); /*  Carregando minha lista com dados */
    estados.Add(new Estados(2, "RJ"));
    estados.Add(new Estados(3, "BA"));


    comboBox1.DataSource = estados; /* Atribuo o DataSource a minha lista */
    comboBox1.ValueMember = "Id"; /* O valor do combox eu pego do "Id" da minha classe Estados */
    comboBox1.DisplayMember = "Sigla"; /* O valor que o usuário irá ver no Combox */

    comboBox1.SelectedValue = 2 /* Selecionei o registro 2 que é igual a "RJ" */
}
 4
Author: Laerte, 2014-01-31 13:04:27

Next, if you click in the list of Items, objects of Type A, you can only use in the property SelectedItem objects that compare to this type.

The following example does not work because of this:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    class MyClass
    {
        private string p;
        public MyClass(string p) { this.p = p; }
        public override string ToString() { return this.p; }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        this.comboBox1.Items.AddRange(new[]
            {
                new MyClass("RJ"),
                new MyClass("MG"),
                new MyClass("SP"),
            });

        this.comboBox1.SelectedItem = "SP"; // tipo string não se compara com tipo MyClass
    }
}

On the other hand, if I insert strings into the list of items, Oh yes, I can use the SelectedItem property with a string, since two strings compare to each other:

        this.comboBox1.Items.AddRange(new[]
            {
                "RJ",
                "MG",
                "SP",
            });

        this.comboBox1.SelectedItem = "SP";
 2
Author: Miguel Angelo, 2014-01-31 12:37:27