views:

129

answers:

2

On a winform there is a combobox that derives its information from a datatable. The datatable draws from a database list.

this.cboList.DataSource = pullData();
this.cboList.DisplayMember = "fieldA";

Once the DataSource is set I am not able to insert a default row (ie *) as the first item in the combobox.

I tried this:

this.cboList.Items.Insert(0,"*");

Is there a way to insert in the combobox after the datasource is set or should this be done in the datatable?

UPDATE1:

The solution looks something like this:

 var list = mydt.AsEnumerable().Select(row => row.Field<string>(fieldName)).ToList();
 list.Insert(0, "*");

Where mydt is a populated datatable and fieldName is a variable holding the database field name.

+2  A: 

Don't modify your data at the source just to make your UI work. Instead, perhaps extract your column into a list that you can modify before attaching it to the combo box.

var list = table.AsEnumerable().Select(row => row.Field<string>("fieldA")).ToList();
list.Insert(0, "*");
this.cboList.DataSource = list;
Anthony Pegram
I am getting a InvalidCastException -> where would I convert/cast a System.Double to System.String in the code you show?
John M
@John - If your column is a double, you would modify it like this `.Select(row => row.Field<double>("fieldA").ToString("0.00")).ToList();`, where "0.00" is the format for your numbers (format can be omitted or altered).
Anthony Pegram
A: 

If a "Select None", or "*" is a valid select option it needs to come from the binding source object. I have done this in the past by adding a default record to a collection before binding it to a combo box.

dretzlaff17