如何在C#中将checkedlistbox绑定到combobox?
我有一个组合框,用户可以选择一个项目,并根据checkedlistbox中的该项目填充一些其他项目如何在C#中将checkedlistbox绑定到combobox?,c#,ms-access,combobox,checkedlistbox,C#,Ms Access,Combobox,Checkedlistbox,我有一个组合框,用户可以选择一个项目,并根据checkedlistbox中的该项目填充一些其他项目 当列中的所有值都不同时,我可以将组合框绑定到数据库中的列。但是,在本例中,该列中有重复的项,我不希望有多个相同类型的项。我只想在组合框中有一个“爬虫”和一个“全地形” 这是用于将源代码绑定到组合框的代码,但不适用于这种情况 comboBox1.ValueMember = "crane index"; comboBox1.DisplayMember = "crane t
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 |
// 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;
//创建一个包含所有起重机类型的列表,但不应包含重复项
列表起重机类型=新列表();
对于(int i=0;i
好的,首先,我的答案很难看,但它有效,而且是我唯一知道的答案,所以我们从您将数据库表导入列表开始
这是一个你可以使用的类
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>();
转到组合框
并订阅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!请一次只问一个问题。尝试为组合框行源选择不同的sql或创建一个查找表用作组合框行源;2.研究级联组合框/列表框谢谢,因为问题是相关的,我问了他们两个。仍然是两个独立的问题。每个问题都有一个独立的解决方案。我已经删除了第二个问题。非常感谢您的帮助。我使用了你的想法,但用另一种方式实现了它。我不确定我的方法是否完全正确或有效。然而,它正在发挥作用。我会用我使用的方法更新我的问题。让我知道你的想法。另外,我还有一个关于这个问题的问题,如果你有时间的话,如果你能帮助我,我会很高兴的。是的,我会很乐意看你的另一个问题,但是你把它贴在哪里?有没有办法我可以直接发送给你?比如电子邮件地址?如果没有,我将把它作为一个新问题发布。@ramtin将它作为一个问题发布,我将在几分钟后检查你的新问题。不幸的是,由于我已经问了2个问题,所以不允许我发布另一个问题。不管怎样,谢谢你的帮助。
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();