UltraGridColumn
ugc = UltraGrid.DisplayLayout.Bands[0].Columns[
"Anteil"];
ugc.MaskDataMode = MaskMode.IncludeLiterals;
ugc.MaskDisplayMode =
MaskMode.IncludeLiterals;
ugc.MinValue = 0;
ugc.MaxValue = 100;
ugc.MaskInput =
ugc.PromptChar = '_';
ugc.CellDisplayStyle =
CellDisplayStyle.FormattedText;
You can hack this out using the IEditorDataFilter interface and the NullText property:
column.NullText = "n/a";column.MaskInput = "nnn.nn\\%";column.Editor.DataFilter = new ZeroToNullDataFilter();
private class ZeroToNullDataFilter : IEditorDataFilter{ object IEditorDataFilter.Convert(EditorDataFilterConvertArgs conversionArgs) { decimal? decimalValue = conversionArgs.Value is decimal ? (decimal)conversionArgs.Value : (decimal?)null;
switch ( conversionArgs.Direction ) { case ConversionDirection.OwnerToEditor: { if ( decimalValue.HasValue && decimalValue.Value == 0m ) { conversionArgs.Handled = true; return System.DBNull.Value; } } break;
case ConversionDirection.EditorToOwner: { if ( conversionArgs.Value == null ) { conversionArgs.Handled = true; return 0m; } } break; }
return conversionArgs.Value; }}
The data filter tricks the column into thinking that zero is null, and the NullText property tells it what to display when the value is null.
Thanks for the "hack" tip!I created a generic version of the ZeroToNullDataFilter but where you specify what value is supposed to be handled as null when displayed. This might be useful for example in the case that you can't or don't want to handle nullable types for your class' properties.
Here is the code (in VB.Net) in case anyone is interested:
'Usage example:column.MaskInput = "{date} {time}"column.Editor.DataFilter = New ValueToNullDataFilter(Of Date)(New Date(0))column.NullText = "N/A"
'CodeImports Infragistics.Win ''' <summary> ''' When using this DataFilter for a value type, the specified "nullValue" is treated as null in the UltraGrid. ''' This enables displaying a specific null text (with the column.NullText property) instead of this "nullValue". ''' </summary> Public Class ValueToNullDataFilter(Of T As Structure) Implements IEditorDataFilter Private _nullValue As T Public Sub New(nullValue As T) _nullValue = nullValue End Sub Public Function Convert(conversionArgs As EditorDataFilterConvertArgs) As Object Implements IEditorDataFilter.Convert Dim value As T? = If(TypeOf (conversionArgs.Value) Is T, CType(conversionArgs.Value, T), CType(Nothing, T?)) Select Case conversionArgs.Direction Case ConversionDirection.OwnerToEditor If value.HasValue AndAlso value.Value.Equals(_nullValue) Then conversionArgs.Handled = True Return System.DBNull.Value End If Case ConversionDirection.EditorToOwner If conversionArgs.Value Is Nothing Then conversionArgs.Handled = True Return _nullValue End If End Select Return conversionArgs.Value End Function End Class
Inspired by all this I also did a default value data filter that fills a cell with a specified default value whenever the user clears the field.
Here is the code, in case anyone is interested:
'Usage example:column.Editor.DataFilter = New DefaultValueDataFilter(Of Date)(Today)
'Code:'(Could loose the Select Case statement for an if statement if you prefer.)Imports Infragistics.Win ''' <summary> ''' Use this as UltraGrid column DataFilter when you want to set a default value ''' each time the user leaves a cell editor as cleared (e.g. mark the value and ''' press delete). ''' </summary> Public Class DefaultValueDataFilter(Of T) Implements IEditorDataFilter Private _defaultValue As T Public Sub New(defaultValue As T) _defaultValue = defaultValue End Sub Public Function Convert(conversionArgs As EditorDataFilterConvertArgs) As Object Implements IEditorDataFilter.Convert Select Case conversionArgs.Direction Case ConversionDirection.OwnerToEditor 'Do Nothing specific Case ConversionDirection.EditorToOwner If IsDBNull(conversionArgs.Value) Then conversionArgs.Handled = True Return _defaultValue End If End Select Return conversionArgs.Value End Function End ClassEnd Namespace