简体   繁体   中英

Filtering ComboBox3 by the selected values of ComboBox1 and ComboBox2

Hello im having a problem with comboBox filtering(cascading). So what i have is :

ComboBox1( that is filled with data from the tblDepartment)

comboBox2 (filed by myself from EDIT ITEMS : 1,2,3,4 for years)

comboBox3 ( shoudl be filtered (depended) from the selection of the comboBox1 and comboBox2.

in comboBox3 i want 2 be listed name_of_course that have the department like comboBox1 and year like comboBox2.

Thanks a lot ,best regards.

Sounds like a LINQ job...

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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM