在ComboBox上列出状态的问题(C#)

时间:2017-11-11 17:02:53

标签: c# winforms

我在ComboBox2上列出Brazillian国家(现在唯一的国家)时遇到了麻烦 我有一个数据库有3个表,Paises(Contries),Estados(States)和Cidades(Cities),我试图让各州使用国家号码并将其列在ComboBox2中,但它不起作用。

我的代码

private void Account_Create_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=WINDOWS-PC\SQLSERVER;Initial Catalog=World;Integrated Security=True");
        string sql = "Select * from Paises";
        SqlCommand cmd = new SqlCommand(sql, con);
        SqlDataReader sdr;

        try
        {
            con.Open();
            sdr = cmd.ExecuteReader();

            while (sdr.Read())
            {
                string Pais = sdr.GetString(1);
                comboBox1.Items.Add(Pais);
            }
        }

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

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=WINDOWS-PC\SQLSERVER;Initial Catalog=World;Integrated Security=True");
        string sql = "Select Cod from Paises where NomePT = '" + comboBox1.Text + "'";
        SqlCommand cmd = new SqlCommand(sql, con);
        SqlDataReader sdr;

        try
        {
            con.Open();
            sdr = cmd.ExecuteReader();

            while (sdr.Read())
            {
                string Cod = sdr.GetValue(0).ToString();
                lbl1.Text = Cod;
            }
        }

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

    }

    private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
    {     
        SqlConnection con = new SqlConnection(@"Data Source=WINDOWS-PC\SQLSERVER;Initial Catalog=World;Integrated Security=True");
        string sql = "select Cod , Estados from Estados where PaisCod = " + lbl1.Text + "";
        SqlCommand cmd = new SqlCommand(sql, con);
        SqlDataReader sdr;

        try
        {
            con.Open();
            sdr = cmd.ExecuteReader();

            while (sdr.Read())
            {          
                string Cod = sdr.GetValue(0).ToString();
                lbl2.Text = Cod;

                string Estado = sdr.GetString(1);
                comboBox2.Items.Add(Estado);     
            }
        }

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

    private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=WINDOWS-PC\SQLSERVER;Initial Catalog=World;Integrated Security=True");
        string sql = "Select * from Cidades where Ci_Cod = " + lbl2.Text + "";
        SqlCommand cmd = new SqlCommand(sql, con);
        SqlDataReader sdr;

        try
        {
            con.Open();
            sdr = cmd.ExecuteReader();

            while (sdr.Read())
            {
                string Cidade = sdr.GetString(1);
                comboBox3.Items.Add(Cidade);  
            }
        }

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

我是C#和Windows Forms的新手,我的代码上有什么东西?

1 个答案:

答案 0 :(得分:0)

我已经想出如何让它发挥作用。 我无法解释我是如何制作它的,但这就是代码:

  private void Form1_Load(object sender, EventArgs e)
    {              
        SqlConnection con = new SqlConnection(@"Data Source=WINDOWS-PC\SQLSERVER;Initial Catalog=World;Integrated Security=True");
        string sql = "Select * from Paises";
        SqlCommand cmd = new SqlCommand(sql, con);
        SqlDataReader sdr;

        try
        {
            con.Open();
            sdr = cmd.ExecuteReader();

            while (sdr.Read())
            {
                string Pais = sdr.GetString(4);
                comboBox1.Items.Add(Pais);
            }                                                                        
        }

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

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=WINDOWS-PC\SQLSERVER;Initial Catalog=World;Integrated Security=True");
        string sql = "Select Cod from Paises where Nome = '" + comboBox1.SelectedItem + "'";
        SqlCommand cmd = new SqlCommand(sql, con);
        SqlDataReader sdr;

        try
        {
            con.Open();
            sdr = cmd.ExecuteReader();

            while (sdr.Read())
            {
                string PaisCod = sdr.GetValue(0).ToString();
                Cod.Text = PaisCod;
            }


            sdr.Close();


            string a = "Select Estados from Estados where PaisCod = " + Cod.Text + "";
            SqlCommand cm1 = new SqlCommand(a , con);
            SqlDataReader sd1;

            sd1 = cm1.ExecuteReader();

            while(sd1.Read())
            {
                string aqw = sd1.GetString(0);
                comboBox2.Items.Add(aqw);
            }

            sd1.Close();     
        }

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

    private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=WINDOWS-PC\SQLSERVER;Initial Catalog=World;Integrated Security=True");
        string b = "Select Cod from Estados where Estados = '" + comboBox2.Text + "'";
        SqlCommand cmd = new SqlCommand(b , con);
        SqlDataReader sdr;

        try
        {
            con.Open();
            sdr = cmd.ExecuteReader();

            while(sdr.Read())
            {
                string Cod = sdr.GetValue(0).ToString();
                Ci_Cod.Text = Cod;
            }

            sdr.Close();

            string c = "Select Cidade from Cidades where Ci_Cod = " + Ci_Cod.Text + "";
            SqlCommand cm1 = new SqlCommand(c , con);
            SqlDataReader sd1;

            sd1 = cm1.ExecuteReader();

            while(sd1.Read())
            {
                string Cidades = sd1.GetString(0);
                comboBox3.Items.Add(Cidades);
            }
        }

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

(是的,我的一些变数很乱) 因此,基本上,当表单加载时,States表加载在CheckBox1上。 当用户在CheckBox上选择国家时,Label Cod接收国家代码,之后,CheckBox2自动选择“PaisCod”等于国家代码的状态,当选择状态时,标签“Ci_Cod”接收状态代码,最后,CheckBox3选择代码等于城市代码的城市。对不起,如果这是令人困惑的,请注意,如果你不理解。