正如标题所示,我希望在值comboBox2
得到更改时显示comboBox1
的值。
For.Eg:我们在comboBox1
(汉堡,冷饮,biryani)中有膳食类型,我们希望在comboBox2
中显示值,就像有人从comboBox1
选择汉堡那样应该是鸡肉汉堡,牛肉汉堡,Zinger汉堡在ComboBox 2中,我们在膳食表中的数据库中提到过,我们还在数据库中制作了一份膳食类型表(膳食类型将在comboBox1
中,而膳食将在{ Visual Studio窗体中的{1}}。
comboBox2
答案 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);
}