0

因此,对于我的组合框,我使用dataSourcewhich contains ID,Name和一个 List ,其中包含一个元素,当我想要空白选择时我需要这些元素。现在,当我打开我的组合框时,我看到了这个:

在此处输入图像描述

--No Material--来自这样的列表:

materialTypes.Insert(0, "-- No Material --");

我需要它,但是ID's名称来自另一个使用 Linq 查询生成的列表,我想隐藏 ID。我不知道方法是什么——隐藏我不需要的数据或明确标记我需要的数据。但我不知道如何做这两件事。

PS这是组合框的全部代码:

 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;
4

1 回答 1

1

Looking at your query it is a bit strange, I would do it like that:

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;
于 2013-02-22T12:28:12.003 回答