2

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();
    }   
}
alhazen
  • 1,907
  • 3
  • 22
  • 43
  • 1
    Hi, I have tried to play with DataGridView and it works for me both ways. Can you provide more code? – Ikaso Mar 01 '10 at 19:50

3 Answers3

0

Once you set all the DataSources, try calling the DataGridView.Refresh() method. This is usually required to display changes to the DataSources.

0

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.

0

I believe the code you have written should work .. just want to know where are you calling the same. It should work if you call it in the databinding_complete event of the grid

V4Vendetta
  • 37,194
  • 9
  • 78
  • 82