简体   繁体   English

c#组合框根据另一个组合框的值过滤一个组合框

[英]c# combobox filtering one combobox based on the value of another combobox

I have 2 tables, product_items and category. 我有2个表格,product_items和category。 And 2 combobox. 和2个组合框。 I want filter another combobox, after selected category. 我要在选择类别之后过滤另一个组合框。 Sorry for my bad english speak :), i tryed this 9h, and no solution get :/ My code how much i have writed. 对不起,我的英语不好说:),我尝试了9h,但没有解决方案:/我的代码我写了多少。

DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter("select itemid, itemname, pcatid from produkti_items_tab", TestConnection);
            da.Fill(ds, "FillDropDown");

            comboBox1.DataSource = ds.Tables["FillDropDown"].DefaultView;
            comboBox1.DisplayMember = "itemname";
            comboBox1.ValueMember = "itemid";

            //============================================
            DataSet ddd = new DataSet();
            SqlDataAdapter dada = new SqlDataAdapter("select pcatid, pcatname from produkti_cat_tab", TestConnection);
            dada.Fill(ddd, "FillDropDownzzz");

            comboBox2.DataSource = ddd.Tables["FillDropDownzzz"].DefaultView;
            comboBox2.DisplayMember = "pcatname";
            comboBox2.ValueMember = "pcatid";

But i dont know what i should write here: 但是我不知道我应该在这里写些什么:

    private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
    //how filter :/
}

Not sure, need to test this, but, the DataView binded to the first combobox has a RowFilter property. 不确定,需要进行测试,但是绑定到第一个组合框的DataView具有RowFilter属性。
Setting this property to the appropriate condition should remove the items that doesn't belong to the current category selected 将此属性设置为适当的条件应删除不属于当前所选类别的项目

if(comboBox2.SelectedValue != null)
{
    DataView dv = comboBox1.DataSource as DataView;
    dv.RowFilter = "pcatid = " + comboBox2.SelectedValue.ToString();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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