如何在winform中显示DataGridViewComboBoxCell值的设置默认值?
我有一个DataGridView
有一个DataGridViewComboBoxColumn
,我已经填充了ComboBox但是在清除DataGridView
我必须为该ComboBox设置一个默认值所以请帮帮我。
您可以在CellFormatting
事件中执行此CellFormatting
void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (e.ColumnIndex == 0) //Index of your DataGridViewComboBoxColumn { e.Value = "Default Value"; } }
我知道这是一个古老的post,但希望我可以帮助一些人避免我的困惑。
使用CellFormatting是一个失败者,因为它会在每次接触到单元格时调用它。 结果是该值不断地设置回默认值。
对我有用的是处理DefaultValuesNeeded事件,如下所示:
private void OnGridDefaultValuesNeeded(object sender, DataGridViewRowEventArgs e) { e.Row.Cells["Position"].Value = PositionEnum.Any; }
这允许我设置默认值,并允许用户更改值。