How to show set default value for DataGridViewComboBoxCell Value in winform?

前端 未结 2 1939
野的像风
野的像风 2021-01-04 20:37

I have a DataGridView having one DataGridViewComboBoxColumn and I have populated that ComboBox but after clear that DataGridView I hav

2条回答
  •  Happy的楠姐
    2021-01-04 21:27

    I know this is an ancient post, but hopefully, I can help some people avoid my confusion.

    Using CellFormatting is a loser because it calls it every time anything touches the cell. The result is that the value gets set back to the default constantly.

    What worked for me was handling the DefaultValuesNeeded event like so:

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

    This allowed me to set the default value, and lets the user change the value.

提交回复
热议问题