9

我有DataGridView一个DataGridViewComboBoxColumn,我已经填充了那个 ComboBox,但是在明确DataGridView我必须为那个 ComboBox 设置一个默认值之后,请帮帮我。

4

2 回答 2

18

我知道这是一篇古老的帖子,但希望我能帮助一些人避免我的困惑。

使用 CellFormatting 是一个失败者,因为它每次接触到单元格时都会调用它。结果是该值不断地设置回默认值。

对我有用的是像这样处理 DefaultValuesNeeded 事件:

private void OnGridDefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
{
    e.Row.Cells["Position"].Value = PositionEnum.Any;
}

这允许我设置默认值,并让用户更改值。

于 2013-07-30T00:52:35.093 回答
7

您可以在CellFormatting活动中执行此操作

void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
      if (e.ColumnIndex == 0) //Index of your DataGridViewComboBoxColumn 
      {
          e.Value = "Default Value";
      }
}
于 2011-12-04T07:03:18.980 回答