Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在两个组合框之间创建关系_C#_Winforms - Fatal编程技术网

C# 在两个组合框之间创建关系

C# 在两个组合框之间创建关系,c#,winforms,C#,Winforms,我有两个相关的表类别和子类别。因此,我在Windows窗体上有两个组合框控件。这两个组合框在父子或类别-子类别关系中相关。例如,我有一个包含类别列表的组合框,供用户从父级中选择,另一个组合框包含子类别列表的子级。 现在我需要:如果用户从第一个组合框中选择类别,那么在第二个组合框中必须出现与该类别相关的子类别。 例如: | Category | cat_id | cat_name | | 1 | Car | | 2 | Car1 | | 3

我有两个相关的表类别和子类别。因此,我在Windows窗体上有两个组合框控件。这两个组合框在父子或类别-子类别关系中相关。例如,我有一个包含类别列表的组合框,供用户从父级中选择,另一个组合框包含子类别列表的子级。 现在我需要:如果用户从第一个组合框中选择类别,那么在第二个组合框中必须出现与该类别相关的子类别。 例如:

| Category
| cat_id   | cat_name |
| 1        | Car      |
| 2        | Car1     |
| 3        | Car2     |
| 4        | Car3     |
和子类别

| SubCategory
| scat_id   | scat_name  | cat_id |
| 1         | sCar       | 1      |
| 2         | sCar1      | 1      |
| 3         | sCar2      | 3      |
| 4         | sCar3      | 1      |
这是表中两个相关的结构。 我有一个C代码:

private void SInfo_Load(object sender, EventArgs e)
{
    using (var context = new StBaseSQLEntities())
    {
        metroComboBox1.DataSource = context.Category.ToList();
        metroComboBox1.DisplayMember = "cat_name";
        metroComboBox1.ValueMember = "cat_id";

        //SubCategory
        metroComboBox2.DataSource = context.SubCategory.ToList();
        metroComboBox2.DisplayMember = "scat_name";
        metroComboBox2.ValueMember = "scat_id";
    }
}
我是C Windows窗体的新手,所以我不知道如何做到这一点。如果从类别组合框中选择1,则第二个组合框需要在子类别组合框中显示属于第一个id的子类别。 如何在C win表单中获得结果?

只需使用第一个组合框上的SelectedValue属性作为子类别的筛选器:

private void MetroComboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
    ComboBox cmb = (ComboBox) sender;
    MetroComboBox2.DataSource = 
                      context.Subcategory.Where(x => x.cat_id == cmb.SelectedValue).ToList();
    MetroComboBox2.enabled = true;
}
。。。如果用户从第一个组合框中选择类别,则在第二个组合框中选择 组合框必须显示与类别相关的子类别

使用 从文档:

当用户更改所选项目且该更改已取消时发生 显示在组合框中

即使以编程方式更改选定值,也会发生其他更改事件

将所有子目录另存为私有成员,这样您就可以在不读取数据库的情况下对其进行筛选

private List<SubCategory> _allSubCategories;

private void SInfo_Load(object sender, EventArgs e)
{
    using (var context = new StBaseSQLEntities())
    {
        metroComboBox1.DataSource = context.Category.ToList();
        metroComboBox1.DisplayMember = "cat_name";
        metroComboBox1.ValueMember = "cat_id";

        //SubCategory
        _allSubCategories = context.SubCategory.ToList();
        metroComboBox2.DataSource = _allSubCategories;
        metroComboBox2.DisplayMember = "scat_name";
        metroComboBox2.ValueMember = "scat_id";
    }
}

您必须使用MetroCombox1 SelectedIndexChanged或SelectedValueChanged事件来使用该值设置MetroCombox2的数据源。我猜那个数据源可能是LINQ'd context.SubCategory.Wherex=>x.scat\u id=metrocombox1.SelectedValue.ToList;诸如此类。对于所选的索引,这是一个危险的假设。@LarsTech你是对的。。。当以这种方式引用id时,会发生令人讨厌的事情。EditedTanks,但我有一个问题,所以在我的表子类别中。cat_id type smallint,如果我运行您的变量,我在var selectedCategory=intcombobox.SelectedValue中有InvalidCastException;将其转换为short然后是的,我尝试过,但它对我不起作用,short type不能与==,.Equals数据类型一起工作等等,我还尝试转换为int 32,但没有帮助。我无法转换where子句中的selectedValue
private void metroComboBox1_SelectionChangeCommitted(object sender, EventArgs e)
{
    var combobox = (ComboBox)sender;
    var selectedCategory = (short)combobox.SelectedValue;
    metroComboBox.DataSource = 
        _allSubCategories.Where(sub => sub.cat_id == selectedCategory).ToList();
    // display/enable item
}