C# – Setting DataGridView.DefaultCellStyle.NullValue to null at designtime raises error at adding rows runtime

cdatagridviewnull

In Visual Studio 2008

  • add a new DataGridView to a form
  • Edit Columns
  • Add a a new DataGridViewImageColumn
  • Open the CellStyle Builder of this column (DefaultCellStyle property)
  • Change the NullValue from System.Drawing.Bitmap to null
  • Try to add a new Row to the DataGridView at runtime
    (dataGridView1.Rows.Add();)
  • You get this error: System.FormatException: Formatted
    value of the cell has a wrong type.

If you change back the NullValue to System.Drawing.Bitmap (as it was) you still get the same error at adding a row.

If you set the NullValue at runtime instead of designtime you don't get anny error.
(dataGridView1.Columns[0].DefaultCellStyle.NullValue = null;)

Could you tell me why is that?

Best Answer

This may well be a bug in the designer; if you take a look around at the .designer.cs file (maybe doing a diff from before and after you set NullValue to null) you should be able to see the code it generates.

Related Topic