Relationship between data with combobox

1

Hello, I have a C # application using Windows Forms

My question would be as follows. I have a database made in Access It contains 2 tables, a table called State, which contains Code and State and another table called Cities, which contains Code, State and City.

Remembering that I have 2 combobox, the first combobox is populated with the data from the State table, using bindingsource, until everything is right, it appears all the status codes. The question I have would be the following, I wanted to make a relationship between combobox 1 and 2, and 2 will show all cities, according to the state selected, but in bindingsource, does not have a filtering option, and I'm not sure how to put it.

Using a search in the database will exit this method:

SELECT Cidade FROM Cidades WHERE Estado='" + sel_UF + "'

I have read some topics on the internet and some here also in stackoverflow, but I could not heal my doubt, in which he talks about putting something related inside the event:

SelectedValueChanged

If someone can give me a light there, thank you in advance

    
asked by anonymous 17.06.2015 / 17:02

2 answers

1

Look if this helps you !!!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace CidadesEstados
{

    public partial class Form1 : Form
    {

        OleDbConnection cnn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=DB.accdb");

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            carregarComboboxEstados();
            cb_estado.Text = "";
        }

        public void carregarComboboxEstados()
        {


          try
            {

                if (!(cnn.State == ConnectionState.Open))
                {
                    cnn.Open();
                }
                System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter("SELECT cod,estado FROM Estado", cnn);
                DataTable dt = new DataTable();
                da.Fill(dt);
                cb_estado.DataSource = dt;
                cb_estado.ValueMember = "estado";
                cb_estado.DisplayMember = "estado";
                cnn.Close();

            }
             catch (Exception ex)
            {
                MessageBox.Show(ex.Message , "Erro");
            }
        }

        public void carregarComboboxCidades()
        {

            try
            {

                if (!(cnn.State == ConnectionState.Open))
                {
                    cnn.Open();
                }
                System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter("SELECT cod,estado,cidade FROM Cidade WHERE estado='" + cb_estado.SelectedValue + "'", cnn);
               DataTable dt = new DataTable();
               da.Fill(dt);
               cb_cidade.DataSource = dt;
               cb_cidade.ValueMember = "estado";
               cb_cidade.DisplayMember = "cidade";
               cnn.Close();

              }
             catch (Exception ex)
            {
                MessageBox.Show(ex.Message , "Erro");
            }

        }

        private void cb_estado_SelectedIndexChanged(object sender, EventArgs e)
        {
            carregarComboboxCidades();
        }


    }
}

If you want to download the project this is the link: Cities and States

Thank you !!

    
26.07.2015 / 18:01
1

Simple

We will call COMBO STATE and CUSTOM COMBO.

You will fill in the combo state as you are doing now with no problems, but will add an event in COMBO STATE, SelectedIndexChanged

In this event, you will load the data from the combo state.

protected virtual void SelectedIndexChanged (EventArgs e){

DDLCidade.DataSrouce = "AQUI VOCÊ MANDA SUA LISTA DE CIDADES FILTRADA";
DDLCidade.DataBind();
}
    
18.06.2015 / 21:54