Hi,
i am fetching the modified date for the records and displaying it in the grid. But, if the modified date is null then the date apperars as 01/01/0001. This is because the DateTime.Parse function will parse the null date returned from the database to 01/01/0001(MinDate). Finally i am binding the entire IList to the grid.
Is there any way or property in XamDataGrid where i can set the date column to null if the date is 01/01/0001?
Any other alternate way is highly appericiated. :)
Thanks & Regards,
Varun R
I created a style that had a text box that was bounded to the value. Then created a date time converter which I use when binding. I wired up the InitializeRecord event and filtered out the cells of the record that where of type DateTime. I then assign the CellValuePresenter of the Cell.Field.Settings to the style.InitializeRecord Method:private void DataGrid1_InitializeRecord(object sender, Infragistics.Windows.DataPresenter.Events.InitializeRecordEventArgs e) { DataRecord record = e.Record as DataRecord; if (record != null) { var dateRecords = from cell in record.Cells where cell.Field.DataType == typeof(DateTime) select cell; foreach (var cell in dateRecords) { cell.Field.Settings.CellValuePresenterStyle = (Style)DataPresenterInstance.Resources["DateTimeFormat"]; } } }Style:<converters:DateTimeConverter x:Key="DateTimeConverter" /><Style TargetType="{x:Type igData:CellValuePresenter}" x:Key="DateTimeFormat"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type igData:CellValuePresenter}"> <Grid Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"> <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content, Converter={StaticResource DateTimeConverter}}"/> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>DateTimeConverter:public class DateTimeConverter: IValueConverter { #region IValueConverter Members public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value == null) return "--/--/----"; DateTime returnVal; if (DateTime.TryParse(value.ToString(), out returnVal)) { if (returnVal != DateTime.MinValue) return returnVal; else return "--/--/----"; } else return "--/--/----"; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value == null) return DateTime.MinValue; DateTime val; if (value.ToString() == "--/--/----") return DateTime.MinValue; if (DateTime.TryParse(value.ToString(), out val)) return val; else return DateTime.MinValue; } #endregion }
Hello Varun,
I believe you can use the InitializeRecord event and see the DataItem's DateTime property and its value and change that accordingly. Another way would be creating a Converter(IValueConverter) for that field and there checking for the date (value).
Hope this helps