Exemple #1
0
        /// <summary>
        /// Resolves the given value or a list of values to the specified format based on the current property configuration.
        /// If the property is restricted or the value is null and the format is string based,
        /// the <c> RestrictedString</c> or <c>NullString</c> are returned respectively.
        /// If the property is multivalued it will try to convert the value to a list or parse it into a list if it's a string
        /// or just add it to a new list as is and then convert each value in the list into the given format.
        /// Otherwise it will try to convert the single value to the given format.
        /// If a custom value converter is set on the property, it will be used first before the default property conversion rules are applied.
        /// </summary>
        /// <param name="value">The value or list of values to resolve to the given format.</param>
        /// <param name="format">The format to convert the value to.</param>
        /// <returns>A value or a list of values resolved to the given format based on the property configuration.</returns>
        public object ResolveValue(object value, ValueFormat format)
        {
            if (IsRestricted())
            {
                return(format.IsString() ? RestrictedString : value);
            }

            if (IsValueNull(value, format))
            {
                return(format.IsString() ? NullString : null);
            }

            if (IsMultiValued)
            {
                IList lst = new List <object>();
                if (value is IList)
                {
                    lst = (IList)value;
                }
                else if (value is string)
                {
                    string[] vals = ((string)value).Split(
                        ParseListSeparators, StringSplitOptions.None);
                    foreach (string val in vals)
                    {
                        if (!IsValueNull(val, format))
                        {
                            lst.Add(val);
                        }
                    }
                }
                else
                {
                    lst.Add(value);
                }
                IList resLst = CreateList(format);
                foreach (object val in lst)
                {
                    object cval = val;
                    if (ValueConverter == null || !ValueConverter(ref cval, format))
                    {
                        cval = ConvertValue(cval, format);
                    }
                    if (!IsValueNull(cval, format))
                    {
                        resLst.Add(cval);
                    }
                }
                return(resLst);
            }
            else
            {
                object cval = value;
                if (ValueConverter == null || !ValueConverter(ref cval, format))
                {
                    cval = ConvertValue(cval, format);
                }
                return(cval);
            }
        }
        /// <summary>
        /// A filtering function that determines if the value of the specified property in the given row
        /// matches the specified criteria value using supplied operator.
        /// </summary>
        /// <param name="property">The data property of the object to match.</param>
        /// <param name="row">The data row to get the value from.</param>
        /// <param name="oper">Comparison operator to use.</param>
        /// <param name="criteria">The value to compare to.</param>
        /// <param name="caseSensitive">True to perform case-sensitive string matching, false otherwise.</param>
        /// <returns>True if the property value in the given row matches the specified criteria, false otherwise.</returns>
        public virtual bool PropertyValueMatches(DataProperty property, DataRow row, Operator oper, object criteria, bool caseSensitive)
        {
            ValueFormat format = criteria?.GetType() == typeof(string) ? ValueFormat.DisplayString : ValueFormat.Internal;
            object      value  = property.GetValue(format, row);

            criteria = property.ResolveValue(criteria, format);
            if (format.IsString() && !caseSensitive)
            {
                value    = value?.ToString()?.ToLower();
                criteria = criteria?.ToString()?.ToLower();
            }
            return(oper.Matches(value, criteria));
        }
        /// <summary>
        /// Resolves the given value or a list of values to the specified format based on the current property configuration.
        /// If the property is restricted or the value is null and the format is string based,
        /// the <c> RestrictedString</c> or <c>NullString</c> are returned respectively.
        /// If the property is multivalued it will try to convert the value to a list or parse it into a list if it's a string
        /// or just add it to a new list as is and then convert each value in the list into the given format.
        /// Otherwise it will try to convert the single value to the given format.
        /// If a custom value converter is set on the property, it will be used first before the default property conversion rules are applied.
        /// </summary>
        /// <param name="value">The value or list of values to resolve to the given format.</param>
        /// <param name="format">The format to convert the value to.</param>
        /// <returns>A value or a list of values resolved to the given format based on the property configuration.</returns>
        public object ResolveValue(object value, ValueFormat format)
        {
            if (IsRestricted())
                return format.IsString() ? RestrictedString : value;

            if (IsValueNull(value, format))
                return format.IsString() ? NullString : null;

            if (IsMultiValued)
            {
                IList lst = new List<object>();
                if (value is IList) lst = (IList)value;
                else if (value is string)
                {
                    string[] vals = ((string)value).Split(
                        ParseListSeparators, StringSplitOptions.None);
                    foreach (string val in vals)
                        if (!IsValueNull(val, format)) lst.Add(val);
                }
                else lst.Add(value);
                IList resLst = CreateList(format);
                foreach (object val in lst)
                {
                    object cval = val;
                    if (ValueConverter == null || !ValueConverter(ref cval, format))
                        cval = ConvertValue(cval, format);
                    if (!IsValueNull(cval, format)) resLst.Add(cval);
                }
                return resLst;
            }
            else
            {
                object cval = value;
                if (ValueConverter == null || !ValueConverter(ref cval, format))
                    cval = ConvertValue(cval, format);
                return cval;
            }
        }
        /// <summary>
        /// Gets the value of the current property in the specified format,
        /// using the provided data row as the data source, if applicable.
        /// If no row is specified, then returns the value of the property itself.
        /// </summary>
        /// <param name="format">The format to return the value in.</param>
        /// <param name="row">The data row to use as a source.</param>
        /// <returns></returns>
        public object GetValue(ValueFormat format, DataRow row = null)
        {
            object val = Column < 0 ? InternalValue : row == null || row.List != parent ? null : row[Column];

            return(format.IsString() ? ValueToString(val, format) : ResolveValue(val, format));
        }