Combobox in Databases

Asked

Viewed 590 times

3

I always used Delphi and there had with component called DBLookupcombobox where it was possible to list the data of a table and use the field ID save in the main table, for example:

In an employee record I have the ID_Cidade that is FK table Cidade, with the DBLookupcombobox I showed the name of the city and in the properties of the component I was able to save the ID_cidade on the table Funcionário, Combobox does it in C#?

Setting it up by properties?

  • 2

    It does. Take a look at this question.

3 answers

2

At the Load event:

cboClientes.DataSource = null;
cboClientes.ValueMember = "id";
cboClientes.DisplayMember = "Nome";
cboClientes.DataSource = _dt;
if (cboClientes.DataSource != null)
{
    cboClientes.SelectedIndex = 0;
}
cboClientes.Refresh();

Just set up the events:

private void cboClientes_SelectedIndexChanged(object sender, EventArgs e)
{
    cboClientes.SelectedValue = idCliente;
}

private void cboClientes_SelectionChangeCommitted(object sender, EventArgs e)
{
    idCliente = (int)cboClientes.SelectedValue;
}

1

A simple way, and I use this in the software I did was the following (I will use as an example a list of Brazilian states, but it may be the data returned from the database):

  1. I read the values from another list/data source and went to the combobox as follows:

    combo.DataSource = new BindingSource(new Dictionary<string, string>()
    {
        { "AC", "AC" },
        { "AL", "AL" },
        { "AP", "AP" },
        { "AM", "AM" },
        { "BA", "BA" },
        { "CE", "CE" },
        { "DF", "DF" },
        { "ES", "ES" },
        { "GO", "GO" },
        { "MA", "MA" },
        { "MT", "MT" },
        { "MS", "MS" },
        { "MG", "MG" },
        { "PA", "PA" },
        { "PB", "PB" },
        { "PR", "PR" },
        { "PE", "PE" },
        { "PI", "PI" },
        { "RJ", "RJ" },
        { "RN", "RN" },
        { "RS", "RS" },
        { "RO", "RO" },
        { "RR", "RR" },
        { "SC", "SC" },
        { "SP", "SP" },
        { "SE", "SE" },
        { "TO", "TO" }
    }, null);
    
    combo.DisplayMember = "Value";
    combo.ValueMember = "Key";
    

Combobox Value, you point it to the Bindingsource of the table you want to save the linked property in Valuemember.

1

Juliano, I made a very simple example using the EntityFramework as data source for the combo box, the complete code you can see here

I also took the opportunity to create an example in ASP.NET MVC, which you can see here

Popular the Combobox

private void Form1_Load(object sender, EventArgs e)
{
    using (ComboBoxDBContext context = new ComboBoxDBContext())
    {
        comboBox1.DataSource = context.ProdutoGrupos.ToList();
        comboBox1.ValueMember = "ProdutoGrupoId";
        comboBox1.DisplayMember = "Descricao";
        comboBox1.Refresh();
    }
}

Recover the Selected Value

private void button1_Click(object sender, EventArgs e)
{
    using (ComboBoxDBContext context = new ComboBoxDBContext())
    {
        Produto produto = new Produto();
        produto.ProdutoGrupoId = Convert.ToInt32(comboBox1.SelectedValue);
        produto.Descricao = textBox1.Text;
        context.Produtos.Add(produto);
        context.SaveChanges();
     }
}

From experience of also being a programmer who left Delphi, I tell you to abstract a little the question of configuration by properties. Not that that’s not possible, but every time I tried to work on Windows Forms linking properties, I got a little frustrated.

Browser other questions tagged

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