本文介绍了如果我选择一个组合框国家不改变另一个组合框中的状态..请帮助我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace country_state_city
{
    public partial class Form1 : Form
    {

        SqlConnection con = new SqlConnection(*************);
        
        public Form1()
        {
            InitializeComponent();
           
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
            SqlCommand cmd = new SqlCommand("select * from Country", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            comboBox1.SelectedIndex = -1;
            comboBox1.DataSource = ds.Tables[0];
           
            comboBox1.DisplayMember = "County";
            comboBox1.ValueMember = "Countryid";
           
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //MessageBox.Show(comboBox1.SelectedIndex.ToString());
            if (comboBox1.SelectedIndex >= 0)
            {

                SqlDataAdapter da = new SqlDataAdapter("Select * From CountryState Where CountryId='" + comboBox1.SelectedValue + "'", con);
                DataSet ds = new DataSet();
                da.Fill(ds);

                comboBox2.DataSource = ds.Tables[0];
                comboBox2.DisplayMember = "State";
                comboBox2.ValueMember = "StateId";
            }
            
        }

        

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox2.SelectedIndex > 0)
            {

                SqlDataAdapter da = new SqlDataAdapter("Select * From StateCity Where StateId='" + comboBox2.SelectedValue + "'", con);
                DataSet ds = new DataSet();
                da.Fill(ds);

                comboBox3.DataSource = ds.Tables[0];
                comboBox3.DisplayMember = "City";
                comboBox3.ValueMember = "CityId";
            }
        }
    }
}

推荐答案

this.comboBox1.SelectedIndexChanged -= new System.EventHandler(this.comboBox1_SelectedIndexChanged);
SqlCommand cmd = new SqlCommand("select * from Country", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();



然后在填充comboBox之后,可以订阅该事件,并强制进行选择,例如


Then after you have populated the comboBox, you can subscribe to the event, and force a selection to happen e.g.

comboBox1.SelectedIndex = -1;
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
comboBox1.SelectedIndex = 0;





2.您的comboBox1将填充数字,因为您使用了



2. Your comboBox1 is going to populated with numbers because you have used

comboBox1.DisplayMember = "County";

- 没有这样的列,它应该是计数 r y



3.永远不要使用字符串连接来构建像这样的SQL语句

- there is no such column, it should be Country

3. Never use string concatenation to build your sql statements like this

SqlDataAdapter da = new SqlDataAdapter("Select * From CountryState Where CountryId='" + comboBox1.SelectedValue + "'", con);

你对自己敞开大门 [] - 使用参数化查询,例如

You leave yourself wide open to SQL Injection attacks[^] - use parameterized queries instead e.g.

SqlCommand cmd = new SqlCommand("Select * from StateCity Where StateId=@id", con);
cmd.Parameters.AddWithValue("@id", comboBox2.SelectedValue);
SqlDataAdapter da = new SqlDataAdapter(cmd);





4.您需要养成使用一些最佳实践的习惯,例如 []和 []


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace country_state_city
{
    public partial class Form1 : Form
    {
        SqlConnection con = new SqlConnection("*************");
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand("select * from Country", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);

            comboBox1.DataSource = ds.Tables[0];

            comboBox1.DisplayMember = "County";
            comboBox1.ValueMember = "Countryid";
            comboBox1.SelectedIndex = 0;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            MessageBox.Show(comboBox1.SelectedIndex.ToString());
            if (comboBox1.SelectedIndex >= 0)
            {
                SqlDataAdapter da = new SqlDataAdapter("Select * From CountryState Where CountryId='" + comboBox1.SelectedValue + "'", con);
                DataSet ds = new DataSet();
                da.Fill(ds);

                comboBox2.DataSource = ds.Tables[0];
                comboBox2.DisplayMember = "State";
                comboBox2.ValueMember = "StateId";
                comboBox2.SelectedIndex = 0;
            }
        }
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox2.SelectedIndex >= 0)
            {
                SqlDataAdapter da = new SqlDataAdapter("Select * From StateCity Where StateId='" + comboBox2.SelectedValue + "'", con);
                DataSet ds = new DataSet();
                da.Fill(ds);
                comboBox3.DataSource = ds.Tables[0];
                comboBox3.DisplayMember = "City";
                comboBox3.ValueMember = "CityId";
                comboBox3.SelectedIndex = 0;
            }
        }
    }
}


这篇关于如果我选择一个组合框国家不改变另一个组合框中的状态..请帮助我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 19:53