0

我正在尝试在 datagridview 组合框中选择一个值,并且从所有谷歌搜索中我认为以下内容应该有效,但事实并非如此。下拉菜单最初是空白的问题。之后我可以手动选择一个值,它会被我的代码保存,但是当尝试恢复它时,组合框中的值是空白的。仅出于测试目的,我手动尝试将值设置为“tag1”,但即使这样也不起作用。

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";
  }
}
4

1 回答 1

1

您应该处理 CellFormatting 事件:

private void OnCellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
      if (e.ColumnIndex == 0) 
      {
          e.Value = "Default_Value";
      }
}
于 2012-01-13T22:44:23.637 回答