Leron
Leron

Reputation: 9866

Windows Forms - adding an empty field to a data bind combobox list

I'm having a combo DropDownList which i fill with data like this:

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

The problem is that I use this DropDownList for search and I want to have one empty row or whatever, just one extra row which I can use to leave the list empty if the user don't want to include this data in a certain search.

Upvotes: 1

Views: 1374

Answers (3)

sa_ddam213
sa_ddam213

Reputation: 43596

I guess you could just add an empty element to your list first then add the items from your context

Example:

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

Upvotes: 1

whastupduck
whastupduck

Reputation: 1166

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

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

This adds a blank item into the list.

Upvotes: 1

Iswanto San
Iswanto San

Reputation: 18569

Try this :

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;

Upvotes: 1

Related Questions