Hi there,
I have a bound DateTime column in my ultragrid where the date is editable. Now I want to show some special dates (e.g. 1/1/1900) with a readonly representation (e.g. simple text like "n/a" or maybe an icon) but i can't achieve this because when I set the value of the cell it has to be a DateTime.
Can anyone help me out?
Achim
This KB article demonstrates how to use the IEditorDataFIlter interface to/from convert strings and booleans. You can use the same approach to solve the problem you describe here (the text aspect of it anyway). This interface allows you to intercept the passing of values between the editor and the display, so you would have the opportunity to convert (for example) the value "n/a" to a DateTime and vice versa.
Note that you can't use this approach to display an icon instead of the value, but the UltraGridCell object exposes an Appearance property which in turn exposes an Image property, and you can assign a value to this property to display an image within the cell next to the value. You could also assign an EmbeddableImageRenderer to the cell's Editor property, and then set its value to an image, which would display ony the image in that cell while allowing the other cells in that column to display a DateTime.
Brian,
thanks for your reply. Your solutions work partly for me (and my colleagues ;) ). Your suggestion using an embeddable image renderer is very interesting. But I did not manage getting it to work. When I assign an Image to the Value of the cell (it is unbound in this case) I get an error that the value has to implement IComparable. So how is the renderer thought to be used?
Thanks for your help.
PS: maybe I have to ask my question in another way: I'm looking for a way to render cell contents in a customized way, regardless if the column is bound or unbound.
Sorry, I was mistaken, apparently you cannot assign an EmbeddableImageRenderer unless the data type is System.Drawing.Image or byte[]. I would have thought that the System.Object data type would be supported, but it isn't.
You can use the IEditorDataFilter interface to covert the data to an Image, so you actually can get both aspects of this to work if you use a data filter. For example, if you made the DataType of the column System.Object (if the column is bound you cannot change the data type, so in that regard this solution is limited to unbound columns, or at least limited to a type that supports both kinds of data), you could assign a EmbeddableImageRenderer to the cell's Editor property, and assign an instance of a class that implements IEditorDataFilter (see below) to that editor's DataFilter property. This would allow the column to support various types, while at the same time allowing that cell to show an image.
public class MyDataFilter : IEditorDataFilter{ object IEditorDataFilter.Convert(EditorDataFilterConvertArgs conversionArgs) { switch ( conversionArgs.Direction ) { case ConversionDirection.OwnerToEditor: { if ( conversionArgs.Editor is EmbeddableImageRenderer ) { conversionArgs.Handled = true; return conversionArgs.Value as Image; } } break; }
return conversionArgs.Value; }}
Thank you for your answer Brian!
That is nearly what I need for my solution. There's only one problem left: since this solution only works on unbound columns, how can I take control over the sorting?
So when I render some special dates as images how can I keep the WinGrid sorting by the DateTime values?
-Achim