C# 选择要在组合框中显示的数据或隐藏所需内容
所以对于我的组合框,我使用数据源,它包含ID、Name和一个列表,其中有一个元素,当我想要空白选择时,我需要这个元素。现在,当我打开组合框时,我看到: -无材料-来自以下列表:C# 选择要在组合框中显示的数据或隐藏所需内容,c#,.net,winforms,C#,.net,Winforms,所以对于我的组合框,我使用数据源,它包含ID、Name和一个列表,其中有一个元素,当我想要空白选择时,我需要这个元素。现在,当我打开组合框时,我看到: -无材料-来自以下列表: materialTypes.Insert(0, "-- No Material --"); 我需要它,但是ID和名称来自使用Linq查询生成的另一个列表,我想隐藏ID。我不知道是什么方法-隐藏我不需要的数据或显式标记我需要的数据。但这两件事我都不知道怎么做 这是组合框的全部代码: IList<String>
materialTypes.Insert(0, "-- No Material --");
我需要它,但是ID和名称来自使用Linq查询生成的另一个列表,我想隐藏ID。我不知道是什么方法-隐藏我不需要的数据或显式标记我需要的数据。但这两件事我都不知道怎么做
这是组合框的全部代码:
IList<String> materialTypes = ((from tom in context.MaterialTypes
where tom.IsActive == true
select tom.Name)
.Union(from tom in context.MaterialTypes
where tom.IsActive == true
select SqlFunctions.StringConvert((double)tom.ID))).ToList();
materialTypes.Insert(0, "-- No Material --");
cboTypeOfMaterial.DataSource = materialTypes;
看看你的提问,有点奇怪,我会这样做:
IList<MaterialType> materialTypes = (from tom in context.MaterialTypes
where tom.IsActive == true
select tom).ToList(); //do you really need to split ID's from Names?
materialTypes.Insert(0, new MaterialType { Name = "-- No Material --" }); //add to our list fake material
combobox.ValueMember = "ID";
combobox.DisplayMember = "Name";
combobox.DataSource = materialTypes;
向我们展示您当前用于填充组合框的代码。我的帖子是用正在使用的代码编辑的。您完全正确。如果你知道我花了多少时间在联合查询上。。。但是谢谢,你的方式好多了。