日期:2014-05-18  浏览次数:20902 次

C#中ComboBox问题
如何让第一个ComboBox值发生改变时,,另一个ComboBox也跟着改。
例如说第一ComboBox是省,,第二ComboBox是城市。第一个更改时,,第二个也要跟着更改。

知道的说一下,,谢谢,,,

------解决方案--------------------
C# code

private void Combobox1.SelectedIndexChanged(object sender, EventArgs e)
{
    .....//目标数据源填充另一个Combobox
}

------解决方案--------------------
C# code

   private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string[] str = {"广州,深圳","福州,厦门" };
            this.comboBox2.Items.Clear();
            if (this.comboBox1.Text == "广东")
            {  
                comboBox2.Items.Add(str[0].Split(',')[0].ToString());
                comboBox2.Items.Add(str[0].Split(',')[1].ToString());

            }
            else
            {
                comboBox2.Items.Add(str[1].Split(',')[0].ToString());
                comboBox2.Items.Add(str[1].Split(',')[1].ToString());
            }
          

        }

------解决方案--------------------
也可以用绑定的,绑定到数据库里。比如有很多城市和省份就可以用绑定比较好。
//加载业务员下拉框
string strSql = "Select EmployeeID,EmployeeName from Employee";
SqlDataAdapter daEmployee = new SqlDataAdapter(strSql,DataBaseOpen.OpenConnection());
daEmployee.Fill(ds, "Employee");
cmbEmployeeID.ValueMember = "EmployeeID";
cmbEmployeeName.ValueMember = "EmployeeName";
cmbEmployeeID.DataSource = ds.Tables["Employee"];
cmbEmployeeName.DataSource = ds.Tables["Employee"];

//加载客户下拉框
strSql = "Select SupplierID,SupplierName from Supplier";
SqlDataAdapter daClient = new SqlDataAdapter(strSql, DataBaseOpen.OpenConnection());
daClient.Fill(ds, "Supplier");
cmbSupplierID.ValueMember = "SupplierID";
cmbSupplierName.ValueMember = "SupplierName";
cmbSupplierID.DataSource = ds.Tables["Supplier"];
cmbSupplierName.DataSource = ds.Tables["Supplier"];