private void SetPropValue(object value)
 {
     if (!this.ControlAtDesignTime())
     {
         this.inSetPropValue = true;
         try
         {
             if ((value == null) || Formatter.IsNullData(value, this.DataSourceNullValue))
             {
                 if (this.propIsNullInfo != null)
                 {
                     this.propIsNullInfo.SetValue(this.control, true);
                 }
                 else if (this.propInfo.PropertyType == typeof(object))
                 {
                     this.propInfo.SetValue(this.control, this.DataSourceNullValue);
                 }
                 else
                 {
                     this.propInfo.SetValue(this.control, null);
                 }
             }
             else
             {
                 this.propInfo.SetValue(this.control, value);
             }
         }
         finally
         {
             this.inSetPropValue = false;
         }
     }
 }
Пример #2
0
        public static object FormatObject(object value,
                                          Type targetType,
                                          TypeConverter sourceConverter,
                                          TypeConverter targetConverter,
                                          string formatString,
                                          IFormatProvider formatInfo,
                                          object formattedNullValue,
                                          object dataSourceNullValue)
        {
            //
            // On the way in, see if value represents 'null' for this back-end field type, and substitute DBNull.
            // For most types, 'null' is actually represented by DBNull. But for a nullable type, its represented
            // by an instance of that type with no value. And for business objects it may be represented by a
            // simple null reference.
            //

            if (Formatter.IsNullData(value, dataSourceNullValue))
            {
                value = System.DBNull.Value;
            }

            //
            // Strip away any use of nullable types (eg. Nullable<int>), leaving just the 'real' types
            //

            Type oldTargetType = targetType;

            targetType      = NullableUnwrap(targetType);
            sourceConverter = NullableUnwrap(sourceConverter);
            targetConverter = NullableUnwrap(targetConverter);

            bool isNullableTargetType = (targetType != oldTargetType);

            //
            // Call the 'real' method to perform the conversion
            //

            object result = FormatObjectInternal(value, targetType, sourceConverter, targetConverter, formatString, formatInfo, formattedNullValue);

            if (oldTargetType.IsValueType && result is null && !isNullableTargetType)
            {
                throw new FormatException(GetCantConvertMessage(value, targetType));
            }

            return(result);
        }
Пример #3
0
        private void SetPropValue(object value)
        {
            // we will not pull the data from the back end into the control
            // when the control is in design time. this is because if we bind a boolean property on a control
            // to a row that is full of DBNulls then we cause problems in the shell.
            if (ControlAtDesignTime())
            {
                return;
            }

            inSetPropValue = true;

            try {
                bool isNull = value == null || Formatter.IsNullData(value, DataSourceNullValue);
                if (isNull)
                {
                    if (propIsNullInfo != null)
                    {
                        propIsNullInfo.SetValue(control, true);
                    }
                    else
                    {
                        if (propInfo.PropertyType == typeof(object))
                        {
                            propInfo.SetValue(control, DataSourceNullValue);
                        }
                        else
                        {
                            propInfo.SetValue(control, null);
                        }
                    }
                }
                else
                {
                    propInfo.SetValue(control, value);
                }
            }
            finally {
                inSetPropValue = false;
            }
        }