fill in combobox with data from other combobox

Asked

Viewed 198 times

0

I have a table City where has the fields City, Status, Idpais, besides the tables State and Country. I’m making an application in c# where I use 3 combobox. When selecting the first combobox (cboCidade) and choosing a record, I want it to be selected at the same time, the respective states and countries of the combobox cboEstado and Cbopais. Recalling that the State and Parents combobox are already filled with the respective values of the tables (State and Parents)

  • Use the event SelectedIndexChanged in the cboCidade doesn’t solve your problem?

  • Add your code to the question so we can indicate the best solution within your scenario.

  • my doubt is precisely in making the code. Besides the value of Valuemenber that the combobox already returns, it would need another value. It would have like that ??

1 answer

-2

In the properties of the cboCidade field, select the radius, where you can configure the events. Then, click 2 x on the Selectedindexchanged item

inserir a descrição da imagem aqui

It will open this code window:

private void cboCidade_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

Inside it, you enter the SQL query to fill in the State and Cbopais combos based on the selected value of cboCity

Here an example:

private void cboCidade_SelectedIndexChanged(object sender, EventArgs e)
    {

        SqlConnection Conexao = new SqlConnection
        {
            ConnectionString = @"sua string de conexao"
        };
        Conexao.Open();

        //Aqui você trará os Ids de Estado e País, para você preencher os respectivos combos
        string ConsultaSql = string.Concat("SELECT idEstado, idPais FROM Cidade WHERE nomeCidade = '", cboCidade.Text, "';");
        SqlCommand ComandoSQl = new SqlCommand(ConsultaSql, ConexaoSql);
        SqlDataReader LeitorDataReader = ComandoSQl.ExecuteReader();

        int estado = 0;
        int pais = 0;

        while (LeitorDataReader.Read())
        {
            estado = Convert.ToInt32(LeitorDataReader["idEstado"].ToString());
            pais = Convert.ToInt32(LeitorDataReader["idPais"].ToString());
        }
        LeitorDataReader.Close();

        //Aqui você irá preencher o combo estado
        ConsultaSql = string.Concat("SELECT nomeEstado FROM Estado WHERE idEstado = ", estado, ";");
        ComandoSQl = new SqlCommand(ConsultaSql, ConexaoSql);
        LeitorDataReader = ComandoSQl.ExecuteReader();
        cboEstado.Items.Clear();
        cboEstado.Text = string.Empty;

        while (LeitorDataReader.Read())
        {
            cboEstado.Items.Add(LeitorDataReader["nomeEstado"].ToString());
        }
        LeitorDataReader.Close();

        //Aqui você irá preencher o combo pais
        ConsultaSql = string.Concat("SELECT nomePais FROM Pais WHERE idPais = ", pais, ";");
        ComandoSQl = new SqlCommand(ConsultaSql, ConexaoSql);
        LeitorDataReader = ComandoSQl.ExecuteReader();
        cboPais.Items.Clear();
        cboPais.Text = string.Empty;

        while (LeitorDataReader.Read())
        {
            cboEstado.Items.Add(LeitorDataReader["nomePais"].ToString());
        }
        LeitorDataReader.Close();

        ConexaoMySql.Close();

    }

Just one remark, please: Adapt SQL objects according to your database (SQL Server, Mysql, etc)

Browser other questions tagged

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