Csharp Fill Combobox with query, but bring already selected the option saved in the bank

Asked

Viewed 1,191 times

1

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

My combobox is filled 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 the product form:

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

These functions fill 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 bring selected the pro_local saved in the bank. How to fill a combobox with a certain ID at the top?

Att.

2 answers

1

You can simply inform on the property SelectedItem the object you want. In the answer you gave, you pass the property pro_local to the property SelectedValue. If the object "product" is contained in DataTable aunica 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.

0

I was able to set the product.pro_local field in Selectedvalue. As it turned out:

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

Browser other questions tagged

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