Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
200
Need to be able to paste in negative values to UltraGrid formated -123 or (123)
posted

How can I do this? I tried setting up a custom Data Filter but it isn't working.

The datatypes in the owner and editor are set to decimal.

 

public class DecimalInputDataFilter : IEditorDataFilter
    {
        private const string DecimalRegexPattern = @"(\-\.?\d{1}|\(\.?\d{1}|\.?\d{1}){1}\d*(\.\d*)?";

        public object Convert(EditorDataFilterConvertArgs args)
        {
            var cell = args.Context as UltraGridCell;

            if (cell == null)
            {
                args.Handled = false;
                return null;
            }

            var value = cell.Value as decimal?;

            switch (args.Direction)
            {
                case ConversionDirection.DisplayToEditor:
                    return CleanInput(value.ToString());
                case ConversionDirection.EditorToDisplay:
                    return value;
                case ConversionDirection.OwnerToEditor:
                    return value;
                case ConversionDirection.EditorToOwner:
                    return value;
            }

            args.IsValid = false;
            return null;
        }

        public static decimal CleanInput(string input)
        {
            if (string.IsNullOrWhiteSpace(input))
                return 0m;

            var match = Regex.Match(input.Replace(","string.Empty), DecimalRegexPattern).Value;

            if (string.IsNullOrWhiteSpace(match))
                return 0m;

            var negative = match.StartsWith(@"-"|| match.StartsWith(@"(");

            decimal result;

            if (negative)
            {
                if (decimal.TryParse(match.Substring(1), out result))
                    result *= -1;
            }
            else
            {
                decimal.TryParse(matchout result);
            }

            return result;
        }
    }