views:

238

answers:

1

The combobox displays a blank field by default even though the combobox is populated with a number of values

ColumnSpeed.DataSource = speedList;
ColumnSpeed.ValueType = typeof(string);

I also tried the following, but it still displays the blank text.

foreach (DataGridViewRow row in myDataGridView.Rows)
{
    DataGridViewComboBoxCell cell = row.Cells[ColumnSpeed.Index] as DataGridViewComboBoxCell;
    if (cell != null)
    {
        cell.DataSource = speedList;
        cell.Value = cell.Items[0].ToString();
    }   
}
A: 

It could be that the ValueMember you assigned to your DataGridView is different from the DisplayMember you assigned. If that's the case, you'll get a blank value, plus you'll get a DataGridError firing.

You should try:

foreach (DataGridViewRow row in dgMain.Rows){
DataGridViewComboBoxCell pkgBoxCell = row.Cells[ColumnSpeed.Index]

pkgBoxCell.Value = ((Package) pkgBoxCell.Items(0)).Id

}

I converted that from vb.net, so it may not compile. In place of the line where i set the value, do whatever steps are necessary to retrieve and set the correct ValueMember value. In my example, I am casting the item to a specific type and using it's id.