Combobox in Database

3

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

In an Employee registry I have the ID_Cidade that is FK of the Cidade table, with DBLookupcombobox I showed the city name and in the component properties I could save the ID_cidade in the Funcionário table, does the Combobox do this in C #?

Configuring it by properties ?

    
asked by anonymous 01.02.2015 / 03:21

3 answers

2

In 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;
}
    
20.05.2015 / 05:54
1

Julian, I made a very simple example using EntityFramework as the 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 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();
    }
}

Retrieve 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 also being a programmer who left Delphi, I tell you to slightly abstract the issue of configuration by properties. Not that this is not possible, but every time I tried to work on Windows Forms linking properties, I was a little frustrated.

    
04.07.2016 / 15:18
1

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

  • I read the values from another list / data source and switched 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";
    
  • Value of the Combobox, you point it to the BindingSource of the table you want to save to the linked property in ValueMember.

        
    04.09.2017 / 14:34