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

送分小仙女□ 提交于 2019-12-18 21:16:53

问题


I have a DataGridView having one DataGridViewComboBoxColumn and I have populated that ComboBox but after clear that DataGridView I have to set one default value for that ComboBox So please help me out.


回答1:


You can do this in CellFormatting event

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



回答2:


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.



来源:https://stackoverflow.com/questions/8373854/how-to-show-set-default-value-for-datagridviewcomboboxcell-value-in-winform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!