Datagridview ComboBox is blank

1.4k Views Asked by At

I am trying to select a value in the datagridview combo box and from all the googling I think the following should work but it isn't. The problem the dropdown menu is blank initially. I can manually pick a value after that, and it gets saved by my code, but when trying to restore it, the value in the combo box is blank. Just for test purposes I am manually trying to set the value as "tag1", but even that isn't working.

DataGridViewComboBoxColumn DropMenu = new DataGridViewComboBoxColumn();
DropMenu.Name = "Tag";
// getListState returns a list of strings
DropMenu.DataSource = SettingsSingelton.Instance.getListState();
DropMenu.ValueType = typeof(string); ;            

dataGridView1.Columns.Add(DropMenu);

for (int i = 0; i < dataGridView1.RowCount && i < storage.Count; i++)
{
  DataGridViewComboBoxCell cell = dataGridView1[3, i] as DataGridViewComboBoxCell;
  if (storage[i].tag != null || storage[i].tag != string.Empty)
  {
    cell.Value = "tag1";
  }
}
1

There are 1 best solutions below

0
On

You should handle the CellFormatting event:

private void OnCellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
      if (e.ColumnIndex == 0) 
      {
          e.Value = "Default_Value";
      }
}