Relationship between data with combobox

Asked

Viewed 1,339 times

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 filled with the data of the state table, using bindingsource, until then everything is right, it appears all the codes of state. The doubt I have would be the following, I wanted to make a relationship between the combobox 1 and 2, and the 2 will show all cities, according to the selected state, but in bindingsource, there is no filtering option, and I am doubtful how to put this.

Using a bank search will exit this method:

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

I read some topics also on the internet and some right here in stackoverflow, but I could not resolve my doubt, in which talks to put something related within the event:

SelectedValueChanged

If anyone can give me a light there, I’d appreciate it

3 answers

1

Simple

We’ll call the State Combo and Citizen Combo.

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

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

protected virtual void SelectedIndexChanged (EventArgs e){

DDLCidade.DataSrouce = "AQUI VOCÊ MANDA SUA LISTA DE CIDADES FILTRADA";
DDLCidade.DataBind();
}
  • I still did not understand, not to mention that your example is in Asp.net, I need something in c# same, for windows form application, and the way that this might not work, because in the bank has all cities and state already, just he knows to select which one he wants

  • Which part you don’t understand?

  • 1

    the concept may even be similar, but in the code declaration is different from Asp to c#

  • Just use the right event that works.

1


Look if it 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!!

  • Help yes, I got by another way, I created a method to search in Fillby, researching only the part of the state and city, but this is also very good

0

private void cadastro_funcionario_Load(object sender, EventArgs e) {
    try {
        OleDbConnection conn = new OleDbConnection(herança.caminho);//herança.caminho é o caminho ca minha base de dados 
        conn.Open();
        string sql = "select * from empresa";
        OleDbDataAdapter adapter = new OleDbDataAdapter(sql, conn);
        DataSet ds = new DataSet();
        adapter.Fill(ds,"empresa");
        comboBox1.DataSource = ds.Tables["empresa"];
        comboBox1.DisplayMember = "nome";
        conn.Close();
        comboBox1.Enabled = true;
    } catch (Exception erro) {
        MessageBox.Show(erro.Message);
    }
}
  • It would be nice if you explained your answer better...

Browser other questions tagged

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