Answer given before the OP indicates that you are using WindowsForms .
Solution for WPF.
Use a ValueConverter to convert DateTime into string .
When doing the conversion if the value of DateTime is DateTime.MinValue
it returns string.Empty;
otherwise it returns dateTime.ToString()
[ValueConversion(typeof(DateTime), typeof(string))]
public class DateTimeToString : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is DateTime)
{
var dateTime = (DateTime)value;
if (dateTime == DateTime.MinValue)
{
return string.Empty;
}
return dateTime.ToString();
}
throw new ArgumentException("value");
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is string)
{
var dateString = (string) value;
if (string.IsNullOrEmpty(dateString))
{
return DateTime.MinValue;
}
return DateTime.Parse(dateString);
}
throw new ArgumentException("value");
}
}
To use the ValueConverter you must first declare it in the <Windos.Resources>
section of your Window .
<Window.Resources>
<local:DateTimeToString x:Key="DateTimeToString" />
</Window.Resources>
Then assign it to the Binding attribute of the DataGridTextColumn
<DataGrid.Columns>
<DataGridTextColumn Header="Data" Binding="{Binding data, Converter={StaticResource DateTimeToString}" />
</DataGrid.Columns>