I have a grid that is bound to a custom class that implements IBindingList.
One of the columns is readonly and displays a DateTime value. I would like to format the DateTime value such that if the value is today, it is "hh:mm:ss". If the date is before today, I would format it "dd/MM/yyyy".
I cannot use the column format as that is not conditional.
I could change the ListObject implementation to return a formatted string, however that breaks the column filtering. In other words, greater than filtering does a string compare, rather that a date compare.
Is there anyway to apply this kind of conditional formatting, or to override the filtering logic, such as using a custom IComparer for filtering.
Thanks.
One way to do this is:
1) Add an UltraDateTimeEditor "dtEditorDefault" to your form, set MaskInput to your default format "dd/MM/yyy". That's the editor most rows will use.
2) Add a second UltraDateTimeEditor "dtEditorToday" to your form, set the MaskInput to "hh:mm:ss". That's the editor you'll use for the cells where the value is today.
3) For the column you want to format, set UseEditorMaskSettings = true and set the EditorControl = dtEditorDefault.
4) Add this to the InitializeRow event handler:
private void ultraGrid1_InitializeRow(object sender, Infragistics.Win.UltraWinGrid.InitializeRowEventArgs e){ UltraGridCell dateCell = e.Row.Cells["MyColumnName"]; if (((DateTime)dateCell.Value) >= DateTime.Today) { dateCell.EditorControl = dtEditorToday; }}
Thanks for the info and the code.
I did try a different idea which seems to work.
I changed the custom class that is used as the row's ListObject, so that rather than returning a DateTime for the columns value, I returned a user defined struct, called DateInfo, which is a wrapper around the DateTime. DateInfo overrides the ToString() to return conditionally formatted value, and implements IComparable for filtering and sorting.
I like this approach, however do you have any opinion which of the 2 would have better performance.
Thanks again
That's fine, as it is readonly regardless.
Your way is more efficient. The down side is that the user will not be able to edit the date.