Answer given before OP indicates you are using Windowsforms.
Solution for WPF.
Use a Valueconverter to convert Datetime in string.
When making the conversion if the value of the Datetime is DateTime.MinValue
return string.Empty;
otherwise return 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 shall first declare it in the section <Windos.Resources>
of his Window.
<Window.Resources>
<local:DateTimeToString x:Key="DateTimeToString" />
</Window.Resources>
Then assign it to the attribute Convert of Binding of Datagridtextcolumn
<DataGrid.Columns>
<DataGridTextColumn Header="Data" Binding="{Binding data, Converter={StaticResource DateTimeToString}" />
</DataGrid.Columns>
Do you use predefined datagriview columns or let it generate the columns automatically?... if it is default... and only edit the column... it has a SUB-property in it called Nullvalue... try to put null or 01/01/0001
– Danilo Breda
Have you ever tried to put this column in your BD as not mandatory and use the type
DateTime?
instead ofDateTime
? The difference is that the first accepts null values unlike the second.– Richard Dias