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

Asked

Viewed 10,204 times

2

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

I’ve inserted all Federation states into the property items of my combobox. When I search for a record saved from my comic book, the combobox does not take on the value.

For example: when searching for a record that contains "AC" in the database table, the combobox does not take value, it goes blank.

The code:

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

cboEstado.SelectedItem = conta.estado;            
  • 1

    Could you elaborate more on your problem? How is this piece of code, for example. This would make it easier to provide an answer that helps you.

  • 2

    I edited the post, thank you.

3 answers

4

You need to assign the Datasource in his Combobox, the class "States" 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 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" */
}

2

Next, if you click on the list of Items, objects of type A, can only be used in the property SelectedItem objects that compare with 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, then yes, I can use the Selecteditem property with a string, as two strings compare to each other:

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

        this.comboBox1.SelectedItem = "SP";

1


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();
  • It was just glue the state.Refresh() that worked. Thanks!

  • In Windows Forms it is necessary to reload the components. In the web environment Databind(); does this work by giving a postback, but many people forget.

Browser other questions tagged

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