如何在winform中显示DataGridViewComboBoxCell值的设置默认值?

Pra*_*war 9 c# datagridview datagridviewcomboboxcell

我有DataGridView一个DataGridViewComboBoxColumn,我已经填充了ComboBox,但在明确DataGridView我必须为该ComboBox设置一个默认值所以请帮助我.

Jam*_*mie 15

我知道这是一个古老的帖子,但希望我可以帮助一些人避免我的困惑.

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

对我有用的是处理DefaultValuesNeeded事件,如下所示:

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

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


Waq*_*qas 7

你可以在CellFormatting活动中这样做

void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
      if (e.ColumnIndex == 0) //Index of your DataGridViewComboBoxColumn 
      {
          e.Value = "Default Value";
      }
}
Run Code Online (Sandbox Code Playgroud)