Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 681
Prakash Kunwar Avatar asked Dec 04 '11 07:12

Prakash Kunwar


2 Answers

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.

like image 138
Jamie Avatar answered Nov 20 '22 12:11

Jamie


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";
      }
}
like image 28
Waqas Avatar answered Nov 20 '22 12:11

Waqas