0

I am working with a DataGridView text column. The column is bound to a decimal column in a DataTable.

If I edit the cell to have an empty value and then tab out I get an exception about an issue with parsing a decimal.

I understand why this is happening and to resolve it I'd like to just force the value to be "0.00" but I can't seem to get that to work.

I tried the following but it didn't work:

private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
    if (e.ColumnIndex == amountIdx)
    {
        if (e.FormattedValue == "")
            dataGridView1[e.ColumnIndex, e.RowIndex].Value = "0.00";
    }
}

Instead of setting the value I can set e.Cancel = true but that just leaves it in edit mode which isn't what I really want.

James
  • 419
  • 4
  • 25

2 Answers2

0

change:

if (e.FormattedValue == "")
    dataGridView1[e.ColumnIndex, e.RowIndex].Value = "0.00";

to:

if (e.FormattedValue == "")
    dataGridView1[e.ColumnIndex, e.RowIndex].Value = "0.00m";

On using "M/m" to annotate decimal values.

Community
  • 1
  • 1
Alex
  • 34,899
  • 5
  • 77
  • 90
0

I figured it out. The value seems to be reset after CellValidating fires. Instead of updating the actual underlying value I had to set the EditingControl back to "0.00". After CellValidating is finished by new value is saved to the data source.

James
  • 419
  • 4
  • 25