如何在C#中将清单框绑定到组合框?

问题描述

我有一个组合框,用户可以选择一个项目,然后根据复选框列表中的该项目,填充一些其他项目。

  1. 当列中的所有值都不相同时,我可以将组合框绑定到数据库中的列。但是,在这种情况下,该列中有重复的项目,因此我不希望有多个相同类型的项目。我只想在组合框中有一个“履带”和一个“全地形”。

这是用于将源绑定到组合框的代码,但在这种情况下不起作用。

comboBox1.ValueMember = "crane index";
comboBox1.DisplayMember = "crane type";
comboBox1.DataSource = test.Tables["cranemaintable"];

此表在数据库中

| Crane Index | Crane Model Number |  Crane Type |
|:-----------:|:------------------:|:-----------:|
| 221         | LR 1400-1          | Crawler     |
| 258         | CC 2800            | Crawler     |
| 262         | CC 2400-1          | All Terrain |
| 265         | CC 6800            | Crawler     |
| 277         | LR 11350           | All Terrain |
  1. 数据库:MS Access
  2. 数据集:测试
  3. 表名:Cranemaintable

我检查了与我的问题有关的其他问题,但找不到我的问题的答案。 您的帮助将不胜感激。 谢谢。

更新: 这就是我发现并为我工作的东西。

// create a list that holds all the crane types but it should not have duplicated items
        List<string> crane_type = new List<string>();
        for (int i = 0; i< test.Tables["cranemaintable"].Rows.Count; i++)
        {
            if (!crane_type.Contains(test.Tables["cranemaintable"].Rows[i]["crane type"]))
            {
                crane_type.Add(Convert.ToString(test.Tables["cranemaintable"].Rows[i]["crane type"]));
            }                
        }        
        //bind the crane_type list to the combobox1
        comboBox1.DataSource = crane_type;

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)