【发布时间】:2020-09-12 17:32:17
【问题描述】:
我有一个组合框,用户可以选择一个项目,并基于选中列表框中的该项目,需要填充一些其他项目。
- 当列中的所有值都不同时,我可以将组合框绑定到数据库中的列。但是,在这种情况下,该列中有重复的项目,我不希望有多个相同类型的项目。我只想在组合框中有一个“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 |
- 数据库: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;
【问题讨论】:
-
欢迎来到 SO!请一次只问 1 个问题。
-
1.尝试为组合框行源选择 DISTINCT sql 或创建一个查找表以用作组合框行源; 2.研究级联combobox/listbox
-
谢谢,因为问题是相关的,所以我问了他们两个。
-
还有 2 个单独的问题。每个都有一个独立于另一个的解决方案。
-
我已经删除了第二个问题。
标签: c# ms-access combobox checkedlistbox