如何解决如何在C#中将清单框绑定到组合框?
我有一个组合框,用户可以选择一个项目,然后根据复选框列表中的该项目,填充一些其他项目。
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 |
- 数据库:MS Access
- 数据集:测试
- 表名: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;
解决方法
好吧,我的答案很丑陋,但它是有效的,而且它是我所知道的唯一答案,所以我们首先从您将数据库表导入列表中开始 这是您可以使用的课程
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();
如果您需要更多帮助或说明,请告诉我。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。