1

In a DataGrid I have a column:

<DataGridTextColumn Header="Freeze First Day" Binding="{Binding FirstDay, StringFormat=\{0:d\}}"/>

and I want to be able to allow the fields to be changed from populated to blank. (FirstDay is of type DateTime.) However, as is, I get an error when I move away from editing that "Value '' could not be converted." I have tried a number of ways to fix this, but I am not seeing finding something that works.

Conrad
  • 2,197
  • 28
  • 53
  • Could you show what kind of data you enter? You can provide a screenshot of the input data and the error, it might help. – Anatoliy Nikolaev Jul 23 '13 at 17:21
  • @AnatoliyNikolaev the problem is when I have an existing, valid date already entered and I want to erase it completely and have no value in the field at all - that is when I get the error. – Conrad Jul 23 '13 at 17:23

2 Answers2

0

Consider to check if the Binding Property is of type nullable DateTime.

private DateTime? firstDay;
public DateTime? FirstDay
{
get
{ 
   return firstDay;
}
set
{
   firstDay=value;
}
}
Arun Selva Kumar
  • 2,593
  • 3
  • 19
  • 30
0

Now, I think - I got root for your issue This is happening because of the StringFormat you had set. Whenever you delete the content the value becomes null. StringFormat cannot process NULL Values so it throws exception.

As an workaround, you can utilize Converter and there you can convert the object using ToString() Method and return if not NULL

//IN THE CONVERTER - YOU CAN RETURN AS,
{
     return (value!=null?value.ToString("{0:d}"):value);
} 
Arun Selva Kumar
  • 2,593
  • 3
  • 19
  • 30
  • Your answer got me most of the way there. I did add a `Converter` and just returned `value` for the `Convert` method, and used the code `return (string)value == "\b" ? DBNull.Value : value;` for the `ConvertBack` method. That did the trick. – Conrad Jul 23 '13 at 19:18