Windows窗体 - 向数据绑定组合框列表添加空字段

时间:2013-02-22 07:32:00

标签: c# visual-studio-2010 data-binding ado.net

我有一个combo DropDownList,我填充了这样的数据:

cboTypeOfMaterial.DataSource = (from tom in context.MaterialTypes
                                where tom.IsActive == true
                                select tom.Name).Distinct().ToList();

问题是我使用这个DropDownList进行搜索,我希望有一个空行或其他任何东西,只有一个额外的行,如果用户不想在列表中包含这些数据,我可以用它来保留列表为空某些搜索。

3 个答案:

答案 0 :(得分:1)

试试这个:

IList<String> materialTypes = (from tom in context.MaterialTypes
                                where tom.IsActive == true
                                select tom.Name).Distinct().ToList();
materialTypes.Insert(0, "-- Please Select --");
cboTypeOfMaterial.DataSource = materialTypes;

答案 1 :(得分:1)

var list = (from tom in context.MaterialTypes
            where tom.IsActive == true
            select tom.Name).Distinct().ToList();

list.Insert(0, "");
cboTypeOfMaterial.DataSource = list;

这会在列表中添加一个空白项目。

答案 2 :(得分:1)

我猜您可以先在列表中添加一个空元素,然后添加上下文中的项目

Example:

cboTypeOfMaterial.DataSource = new string[] { string.Empty }
                               .Concat(from tom in context.MaterialTypes
                                       where tom.IsActive == true
                                       select tom.Name).Distinct().ToList();
相关问题