[英]Filtering ComboBox3 by the selected values of ComboBox1 and ComboBox2
Hello im having a problem with comboBox filtering(cascading). 您好,我对comboBox过滤(层叠)有问题。 So what i have is :
所以我有:
ComboBox1( that is filled with data from the tblDepartment) ComboBox1(由tblDepartment中的数据填充)
comboBox2 (filed by myself from EDIT ITEMS : 1,2,3,4 for years) comboBox2(由我自己从编辑项目中归档:1,2,3,4年)
comboBox3 ( shoudl be filtered (depended) from the selection of the comboBox1 and comboBox2. comboBox3(应从comboBox1和comboBox2的选择中进行过滤(取决于)。
in comboBox3 i want 2 be listed name_of_course that have the department like comboBox1 and year like comboBox2. 在comboBox3中,我希望列出2的name_of_course,其部门如comboBox1,年份如comboBox2。
Thanks a lot ,best regards. 非常感谢。
Sounds like a LINQ job... 听起来像LINQ的工作...
You could try something like 您可以尝试类似
comboBox3.DataSource = from c in courses
where c.department == comboBox1.Text
&& c.year == int.Parse(comboBox2.Text)
select c.name;
Yo can use SelectedItem
property of the ComboBox
to get the selected item. 您可以使用
ComboBox
SelectedItem
属性来获取所选项目。
Try This: 尝试这个:
private void button1_Click(object sender, EventArgs e)
{
comboAll.Items.Add(comboCourseName.SelectedItem.ToString()+" "+comboYear.SelectedItem.ToString());
}
SqlConnection conn = new SqlConnection("Data Source=jaci;Initial Catalog=projecttest;Integrated Security=True");
string query = string.Format("SELECT name_of_course FROM course WHERE kathedra='" + comboBox1.Text + "' AND year='" + comboBox2.Text + "'");
SqlCommand cmd = new SqlCommand(query);
DataTable dt = new DataTable();
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(query, conn);
adapter.Fill(dt);
ds.Tables.Add(dt);
foreach (DataRow dr in dt.Rows)
{
comboBox3.Items.Add(dr[0].ToString());
}
I already fixed the problem, but now I'm facing a new problem, when I change the selected item on the Cathedra, I get the previous items in the comboBox3, so I need to make a statement to clear the items but I don't know where to place it - (comboBox3.items.Clear). 我已经解决了这个问题,但是现在我遇到了一个新问题,当我在Cathedra上更改选定的项目时,我在comboBox3中得到了以前的项目,因此我需要声明以清除这些项目,但是我没有这样做不知道将它放在哪里-(comboBox3.items.Clear)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.