如果我选择一个组合框国家不改变另一个组合框中的状态..请帮助我 [英] Whrn I Select One Combo Box Country Not Change State In Another Combo Box.. Pls Help Me

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

问题描述

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";
            }
        }
    }
}

推荐答案

这里要考虑几件事情...



1.你的 comboBox1_SelectedIndexChanged 事件在正在填充comboBox时被触发,而 SelectedIndex 此时等于零。所以处理正在落入你的代码中,但是在选择实际之前。



解决这个问题的一种方法是取消订阅<$在填充comboBox时c $ c> comboBox1_SelectedIndexChanged 事件。例如像这样
Couple of things to consider here ...

1. Your comboBox1_SelectedIndexChanged event is being fired as the comboBox is being populated, and the SelectedIndex equals zero at that point. So processing is dropping into your code, but before there is anything actually selected.

One way around this is to unsubscribe from the comboBox1_SelectedIndexChanged event while you are populating the comboBox. e.g. like this
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);

你对自己敞开大门 SQL注入攻击 [ ^ ] - 使用参数化查询,例如

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.您需要养成使用一些最佳实践的习惯,例如 Try-Catch块 [ ^ ]和使用语句 [ ^ ]


你好





请参阅我发布的以下代码



Hello


See the below code posted by me

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;
            }
        }
    }
}


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆