Reputation: 1200
I'm programmatically setting up my datagrid columns for my datagrid, then binding the grid to an observable collection.
A couple of my columns are bound to DateTime properties, however, if they're null values in the database, it's setting the DateTime properties to the min value due to DateTime being a non nullable type.
My binding is as follows for the aforementioned columns:
DataGridTextColumn scanned = new DataGridTextColumn();
scanned.Header = "Scanned";
scanned.Binding = new Binding("DateScanned");
dataGrid.Columns.Add(scanned);
"DateScanned" being the datetime property. Now, instead of those values displaying in the grid as "1/1/0001", I'd prefer it if they were blanked out. Herein lies my question.
Can I set this binding up to be conditional somehow and if the property value is "1/1/0001", display nothing?
Upvotes: 0
Views: 1111
Reputation: 62246
I think you should use Converter in order to convert from you ModelView to View the data in format you want. Have a look on this simple tutorial :
Regards.
Upvotes: 1
Reputation: 1200
Nevermind, found out about binding converters. Wrote a "MinDateConverter" that checks if it's the min date time. If it is, it returns blank, otherwise it returns the datetime.
public class MinDateConverter : IValueConverter
{
public MinDateConverter()
{
}
public virtual object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
DateTime dt = (DateTime)value;
if (dt.Equals(DateTime.MinValue))
return string.Empty;
else
return dt.ToShortDateString();
}
public virtual object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value;
}
}
Upvotes: 0
Reputation: 11210
Use a value converter to convert specific values like that to blank or whatever else you want.
Upvotes: 1