当ComboBox 1的值发生变化时,如何在ComboBox 2中更改值(两个组合框的值都在SQL数据库中)

时间:2014-11-14 06:59:23

标签: c# sql database combobox windows-forms-designer

正如标题所示,我希望在值comboBox2得到更改时显示comboBox1的值。

For.Eg:我们在comboBox1(汉堡,冷饮,biryani)中有膳食类型,我们希望在comboBox2中显示值,就像有人从comboBox1选择汉堡那样应该是鸡肉汉堡,牛肉汉堡,Zinger汉堡在ComboBox 2中,我们在膳食表中的数据库中提到过,我们还在数据库中制作了一份膳食类型表(膳食类型将在comboBox1中,而膳食将在{ Visual Studio窗体中的{1}}。

comboBox2

1 个答案:

答案 0 :(得分:0)

为你的Combobox制作两种方法,并在构造函数中调用你的膳食类型jcombobox 这是样本

     public ordermeal() 
     {
        InitializeComponent();
        yourMealtypeCombobox();
     }


       public void yourMealtypeCombobox()
        {
            DataTable dt = md.GetALLMealTypes(); // 
            comboBox1.DataSource = dt;
            comboBox1.DisplayMember = "typename";
            comboBox1.ValueMember = "mealtypeid";
        }



    public void yourMealCombo(int mealtypeid)
            {
                DataTable dt = md.GetMeals(mealtypeid); // Get your corresponding meal by mealtypeid of Selected mealtype by users
/* i am assuming your GetMeals() method is able to except input parameter and filter records from input parameter */
                comboBox2.DataSource = dt;
                comboBox2.DisplayMember = "Mealname";
                comboBox2.ValueMember = "unitprice";

            }

并调用组合框选择更改两个combobox1的提交事件

    private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
    {
      // here Get the Selected Type of of meal
      int mealTypeId = Convert.ToInt32(comboBox1.SelectedValue);// here i am not checking null but you should do it
     // finaly call mealcombo from here
     yourMealCombo(mealTypeId);
    }