【问题标题】:How to bind a checkedlistbox to a combobox in C#?如何将选中的列表框绑定到 C# 中的组合框?
【发布时间】:2020-09-12 17:32:17
【问题描述】:

我有一个组合框,用户可以选择一个项目,并基于选中列表框中的该项目,需要填充一些其他项目。

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

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

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;

【问题讨论】:

  • 欢迎来到 SO!请一次只问 1 个问题。
  • 1.尝试为组合框行源选择 DISTINCT sql 或创建一个查找表以用作组合框行源; 2.研究级联combobox/listbox
  • 谢谢,因为问题是相关的,所以我问了他们两个。
  • 还有 2 个单独的问题。每个都有一个独立于另一个的解决方案。
  • 我已经删除了第二个问题。

标签: c# ms-access combobox checkedlistbox


【解决方案1】:

好的,首先我的回答很丑,但它有效,而且它是我知道的唯一一个,所以我们首先将你的数据库表导入列表 这是一个你可以使用的类

public class myclass
   {
      public string CraneIndex { get; set; }
      public string CraneModelNumber { get; set; }
      public string CraneType { get; set; }
   }

然后列出 3 个列表

  List<myclass> myclasses = new List<myclass>();
  List<myclass> myclasses1 = new List<myclass>();
  List<myclass> myclasses2 = new List<myclass>();

在这里,您将表导入列表myclasses,然后我们过滤来自数据库的主列表以仅具有唯一类型

      myclasses1 = myclasses.GroupBy(myclass => myclass.CraneType).Select(x => x.First()).ToList();
      comboBox1.DataSource = myclasses1;
      comboBox1.DisplayMember = "CraneType";

转到combobox 并订阅selectedindexchanged 属性并将其添加到其函数中

 myclasses2.Clear();
    foreach (var item in myclasses)
    {
       if (comboBox1.Text==item.CraneType)
       {
         myclasses2.Add(item);
       }
    }
    checkedListBox1.Items.Clear();
    foreach (var item in myclasses2)
    {
        checkedListBox1.Items.Add(item.CraneModelNumber);
    }
    this.checkedListBox1.DisplayMember = "cranemodels";
    checkedListBox1.Refresh();

如果您需要更多帮助或解释,请告诉我。

【讨论】:

  • 非常感谢您的帮助。我使用了您的想法,但以另一种方式实现了它。我不确定我的方式是否完全正确或有效。但是,它正在工作。我会用我用过的方法更新我的问题。让我知道你的想法。另外我还有一个与此问题相关的问题,如果您有时间可以帮助我,我会很高兴。
  • 是的,我很乐意看看您的其他问题,但您在哪里发布?
  • 有什么方法可以直接发给你吗?像电子邮件地址?如果没有,我会将它作为一个新问题发布在 SO 上。
  • @ramtin 将其作为问题发布,我会在几分钟内检查您的新问题
  • 不幸的是,SO 不允许我发布另一个问题,因为到目前为止我已经问了 2 个问题。无论如何,谢谢你的帮助。
猜你喜欢
  • 1970-01-01
  • 2010-10-10
  • 1970-01-01
  • 2014-03-16
  • 1970-01-01
  • 1970-01-01
  • 2011-09-01
  • 2018-10-23
相关资源
最近更新 更多