Example #1
0
        /// <summary>
        /// Sets the text to be displayed in the edit control.
        /// Depending on the type of the edit control, this sometimes can be
        /// done in various ways.
        /// </summary>
        /// <param name="text">The text to display.</param>
        private void DisplayEditControlText(string text)
        {
            try
            {
                MultilineEditTextBox tbmInPlaceEditor = activeEditControl as MultilineEditTextBox;
                if (tbmInPlaceEditor != null)
                {
                    // In-place edit control is a MultilineEditTextBox
                    tbmInPlaceEditor.MultiLineText = text;
                    //tbmInPlaceEditor.SelectAll();
                    return;
                }

                TextBox tbInPlaceEditor = activeEditControl as TextBox;
                if (tbInPlaceEditor != null)
                {
                    // In-place edit control is a TextBox
                    tbInPlaceEditor.Text = text;
                    tbInPlaceEditor.SelectAll();
                    return;
                }

                ComboBox comboInPlaceEditor = activeEditControl as ComboBox;
                if (comboInPlaceEditor != null)
                {
                    // In-place edit control is a ComboBox
                    comboInPlaceEditor.Text = text;
                    return;
                }

                DateTimePicker dtp = activeEditControl as DateTimePicker;
                if (dtp != null)
                {
                    DateTimeConverter dtc = new DateTimeConverter();
                    dtp.Value = (DateTime)dtc.ConvertFromInvariantString(text);
                    return;
                }

                NumericUpDown nud = activeEditControl as NumericUpDown;
                if (nud != null)
                {
                    DecimalConverter dc = new DecimalConverter();
                    nud.Value = (decimal)dc.ConvertFromInvariantString(text);
                    return;
                }
            }
            catch (Exception ex)
            {
                ErrorDispatcher.DispatchError(ex);
            }
        }
        /// <internalonly/>
        /// <devdoc>
        /// </devdoc>
        public static object ConvertValue(object value, Type toType, string formatString) {
            // Workaround for now to handle inability of reflection to deal with Null.
            if (value == null || Convert.IsDBNull(value)) {
                return value;
            }

            Type fromType = value.GetType();

            if (toType.IsAssignableFrom(fromType)) {
                return value;
            }

            // for now, just hit the sweet spots.
            if (typeof(string).IsAssignableFrom(fromType)) {
                if (typeof(int).IsAssignableFrom(toType)) {
                    return Convert.ToInt32((string)value, CultureInfo.InvariantCulture);
                }
                else if (typeof(bool).IsAssignableFrom(toType)) {
                    return Convert.ToBoolean((string)value, CultureInfo.InvariantCulture);
                }
                else if (typeof(DateTime).IsAssignableFrom(toType)) {
					return Convert.ToDateTime((string)value, CultureInfo.InvariantCulture);
                }
                else if (typeof(Decimal).IsAssignableFrom(toType)) {
                    TypeConverter tc = new DecimalConverter();                
                    return tc.ConvertFromInvariantString((string)value);
                }
                else if (typeof(Double).IsAssignableFrom(toType)) {
					return Convert.ToDouble((string)value, CultureInfo.InvariantCulture);
                }
                else if (typeof(Int16).IsAssignableFrom(toType)) {
					return Convert.ToInt16((Int16)value, CultureInfo.InvariantCulture);
                }
                else {
                    throw new ArgumentException(
                                               SR.GetString(SR.Cannot_convert_from_to, fromType.ToString(), toType.ToString()));
                }
            }
            else if (typeof(string).IsAssignableFrom(toType)) {
                if (typeof(int).IsAssignableFrom(fromType)) {
                    return ((int)value).ToString(formatString, CultureInfo.InvariantCulture);
                }
                else if (typeof(bool).IsAssignableFrom(fromType)) {
                    string [] tokens=null;

                    if (formatString != null) {
                        tokens = formatString.Split(formatSeparator);
                        if (tokens.Length != 2) {
                            tokens = null;
                        }
                    }

                    if ((bool)value) {
                        if (tokens != null) {
                            return tokens[0];
                        }
                        else {
                            return "true";
                        }
                    }
                    else {
                        if (tokens != null) {
                            return tokens[1];
                        }
                        else {
                            return "false";
                        }
                    }
                }
                else if (typeof(DateTime).IsAssignableFrom(fromType)) {
                    return((DateTime)value).ToString(formatString, CultureInfo.InvariantCulture);
                }
                else if (typeof(Decimal).IsAssignableFrom(fromType)) {
                    return ((Decimal)value).ToString(formatString, CultureInfo.InvariantCulture);
                }
                else if (typeof(Double).IsAssignableFrom(fromType)) {
                    return ((Double)value).ToString(formatString, CultureInfo.InvariantCulture);
                }
                else if (typeof(Single).IsAssignableFrom(fromType)) {
                    return ((Single)value).ToString(formatString, CultureInfo.InvariantCulture);
                }
                else if (typeof(Int16).IsAssignableFrom(fromType)) {
                    return ((Int16)value).ToString(formatString, CultureInfo.InvariantCulture);
                }
                else {
                    throw new ArgumentException(
                                               SR.GetString(SR.Cannot_convert_from_to, fromType.ToString(), toType.ToString()));
                }
            }
            else {
                throw new ArgumentException(
                                           SR.GetString(SR.Cannot_convert_from_to, fromType.ToString(), toType.ToString()));
            }
        }
 public static object ConvertValue(object value, Type toType, string formatString)
 {
     if ((value == null) || Convert.IsDBNull(value))
     {
         return value;
     }
     Type c = value.GetType();
     if (toType.IsAssignableFrom(c))
     {
         return value;
     }
     if (typeof(string).IsAssignableFrom(c))
     {
         if (typeof(int).IsAssignableFrom(toType))
         {
             return Convert.ToInt32((string) value, CultureInfo.InvariantCulture);
         }
         if (typeof(bool).IsAssignableFrom(toType))
         {
             return Convert.ToBoolean((string) value, CultureInfo.InvariantCulture);
         }
         if (typeof(DateTime).IsAssignableFrom(toType))
         {
             return Convert.ToDateTime((string) value, CultureInfo.InvariantCulture);
         }
         if (typeof(decimal).IsAssignableFrom(toType))
         {
             TypeConverter converter = new DecimalConverter();
             return converter.ConvertFromInvariantString((string) value);
         }
         if (typeof(double).IsAssignableFrom(toType))
         {
             return Convert.ToDouble((string) value, CultureInfo.InvariantCulture);
         }
         if (!typeof(short).IsAssignableFrom(toType))
         {
             throw new ArgumentException(System.Web.SR.GetString("Cannot_convert_from_to", new object[] { c.ToString(), toType.ToString() }));
         }
         return Convert.ToInt16((short) value, CultureInfo.InvariantCulture);
     }
     if (!typeof(string).IsAssignableFrom(toType))
     {
         throw new ArgumentException(System.Web.SR.GetString("Cannot_convert_from_to", new object[] { c.ToString(), toType.ToString() }));
     }
     if (typeof(int).IsAssignableFrom(c))
     {
         int num = (int) value;
         return num.ToString(formatString, CultureInfo.InvariantCulture);
     }
     if (typeof(bool).IsAssignableFrom(c))
     {
         string[] strArray = null;
         if (formatString != null)
         {
             strArray = formatString.Split(formatSeparator);
             if (strArray.Length != 2)
             {
                 strArray = null;
             }
         }
         if ((bool) value)
         {
             if (strArray != null)
             {
                 return strArray[0];
             }
             return "true";
         }
         if (strArray != null)
         {
             return strArray[1];
         }
         return "false";
     }
     if (typeof(DateTime).IsAssignableFrom(c))
     {
         DateTime time = (DateTime) value;
         return time.ToString(formatString, CultureInfo.InvariantCulture);
     }
     if (typeof(decimal).IsAssignableFrom(c))
     {
         decimal num2 = (decimal) value;
         return num2.ToString(formatString, CultureInfo.InvariantCulture);
     }
     if (typeof(double).IsAssignableFrom(c))
     {
         double num3 = (double) value;
         return num3.ToString(formatString, CultureInfo.InvariantCulture);
     }
     if (typeof(float).IsAssignableFrom(c))
     {
         float num4 = (float) value;
         return num4.ToString(formatString, CultureInfo.InvariantCulture);
     }
     if (!typeof(short).IsAssignableFrom(c))
     {
         throw new ArgumentException(System.Web.SR.GetString("Cannot_convert_from_to", new object[] { c.ToString(), toType.ToString() }));
     }
     short num5 = (short) value;
     return num5.ToString(formatString, CultureInfo.InvariantCulture);
 }