CSharp Fill ComboBox with query, however bring already selected the option saved in the bank

1

I have a product registration screen with a LOCAL Combobox. Product table has LOCAL ID, called pro_local. And the local table the fields loc_cod and loc_descricao

My combobox is populated with the function:

public DataTable RetornaLocal()
    {
        SqlConnection sqlConnection = acessoDadosSqlServer.CriarConexao();
        sqlConnection.Open();
        SqlCommand sqlCommand = sqlConnection.CreateCommand();
        sqlCommand.CommandText = "SELECT * FROM local ORDER BY loc_descricao";

        SqlDataReader sqlDataReader = null;
        sqlDataReader = sqlCommand.ExecuteReader();

        DataTable dataTable = new DataTable();

        dataTable.Load(sqlDataReader);
        return dataTable;

    }

In Product Form:

            cbLocal.DisplayMember = "loc_descricao";
            cbLocal.ValueMember = "loc_cod";
            cbLocal.DataSource = localNegocios.RetornaLocal();

These functions populate the combobox, but when I bring the product form to change this product, it brings the combobox as if it were an insert, it does not have the selected pro_local saved in the database. How do I populate a combobox with a given ID at the top?

Att.

    
asked by anonymous 31.07.2015 / 19:11

2 answers

1

You can simply enter in the SelectedItem property the object you want. In the response you gave, you pass the pro_local property to the SelectedValue property. If the "product" object is contained in DataTable even thing you need to do is comboBox2.SelectedItem = produto; . I usually work with typed lists so I never tested with DataTable but I believe it would be the same.

    
31.07.2015 / 20:31
0

I was able to set the Product.pro_local field in the SelectedValue. How was it?

        cbLocal.DisplayMember = "loc_descricao";
        cbLocal.ValueMember = "loc_cod";
        cbLocal.DataSource = localNegocios.RetornaLocal();
        cbLocal.SelectedValue = produto.pro_local ;
    
31.07.2015 / 19:31