public static void PopulateEntityProperty(Control control, PropertyInfo p, object entity, bool treatZeroAsNull) { bool isNullable = p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>); Type controlType = control.GetType(); string controlTypeName = controlType.FullName; string propertyTypeName = p.PropertyType.FullName; if (controlTypeName.Equals(typeof(TextBox).FullName)) { TextBox textBox = (TextBox)control; p.SetValue(entity, textBox.Text, null); } else if (controlTypeName.Equals(typeof(NumericTextBoxWindows).FullName)) { NumericTextBoxWindows numericTextBox = (NumericTextBoxWindows)control; object value = DataHelperWindows.ChangeType(numericTextBox.Value, p.PropertyType); if (treatZeroAsNull && value != null && value.ToString() == "0") { value = null; } p.SetValue(entity, value, null); } else if (controlTypeName.Equals(typeof(CheckBox).FullName)) { CheckBox checkBox = (CheckBox)control; p.SetValue(entity, checkBox.Checked, null); } else if (controlTypeName.Equals(typeof(NumericUpDown).FullName)) { NumericUpDown numericUpDown = (NumericUpDown)control; object value = DataHelperWindows.ChangeType(numericUpDown.Value, p.PropertyType); if (treatZeroAsNull && value != null && value.ToString() == "0") { value = null; } p.SetValue(entity, value, null); } else if (controlTypeName.Equals(typeof(DateTimePicker).FullName)) { DateTimePicker dateTimePicker = (DateTimePicker)control; p.SetValue(entity, dateTimePicker.Value, null); } else if (controlTypeName.Equals(typeof(ComboBox).FullName)) { ComboBox comboBox = (ComboBox)control; Array enumValues = EnumHelperWindows.GetEnumValues(p.PropertyType); p.SetValue(entity, comboBox.SelectedItem, null); } else { throw new Exception(string.Format( "Unexpected controltype {0} to be used to populate property {1} on {2}.", controlTypeName, p.Name, entity.GetType().FullName)); } }
public static object GetControlValue(Control control, PropertyInfo p, bool treatZeroAsNull) { bool isNullable = p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>); Type controlType = control.GetType(); string controlTypeName = controlType.FullName; string propertyTypeName = p.PropertyType.FullName; if (controlTypeName.Equals(typeof(TextBox).FullName)) { TextBox textBox = (TextBox)control; return(textBox.Text); } else if (controlTypeName.Equals(typeof(NumericTextBoxWindows).FullName)) { NumericTextBoxWindows numericTextBox = (NumericTextBoxWindows)control; object value = DataHelperWindows.ChangeType(numericTextBox.Value, p.PropertyType); if (treatZeroAsNull && value != null && value.ToString() == "0") { value = null; } return(value); } else if (controlTypeName.Equals(typeof(CheckBox).FullName)) { CheckBox checkBox = (CheckBox)control; return(checkBox.Checked); } else if (controlTypeName.Equals(typeof(NumericUpDown).FullName)) { NumericUpDown numericUpDown = (NumericUpDown)control; object value = DataHelperWindows.ChangeType(numericUpDown.Value, p.PropertyType); if (treatZeroAsNull && value != null && value.ToString() == "0") { value = null; } return(value); } else if (controlTypeName.Equals(typeof(DateTimePicker).FullName)) { DateTimePicker dateTimePicker = (DateTimePicker)control; return(dateTimePicker.Value); } else if (controlTypeName.Equals(typeof(ComboBox).FullName)) { ComboBox comboBox = (ComboBox)control; Array enumValues = EnumHelperWindows.GetEnumValues(p.PropertyType); return(comboBox.SelectedItem); } else { throw new Exception(string.Format( "Unexpected controltype {0} to be used to retrieve control value using property type {1}.", controlTypeName, p.Name)); } }
public static void PopulateControl(Control control, object value) { if (value == null) { return; } Type controlType = control.GetType(); string controlTypeName = controlType.FullName; if (controlTypeName.Equals(typeof(TextBox).FullName)) { TextBox textBox = (TextBox)control; textBox.Text = value.ToString(); } else if (controlTypeName.Equals(typeof(NumericTextBoxWindows).FullName)) { NumericTextBoxWindows numericTextBox = (NumericTextBoxWindows)control; numericTextBox.Value = Convert.ToDouble(value); } else if (controlTypeName.Equals(typeof(CheckBox).FullName)) { CheckBox checkBox = (CheckBox)control; checkBox.Checked = Convert.ToBoolean(value); } else if (controlTypeName.Equals(typeof(NumericUpDown).FullName)) { NumericUpDown numericUpDown = (NumericUpDown)control; numericUpDown.Value = Convert.ToDecimal(value); } else if (controlTypeName.Equals(typeof(DateTimePicker).FullName)) { DateTimePicker dateTimePicker = (DateTimePicker)control; dateTimePicker.Value = Convert.ToDateTime(value); } else if (controlTypeName.Equals(typeof(ComboBox).FullName)) { ComboBox comboBox = (ComboBox)control; Array enumValues = EnumHelperWindows.GetEnumValues(value.GetType()); for (int i = 0; i < enumValues.Length; i++) //Populate the ComboBox with all the enum values. { object e = enumValues.GetValue(i); comboBox.Items.Add(e); } comboBox.SelectedItem = value; //Set the selected item based on the passed in value. } else { throw new Exception(string.Format( "Unexpected controltype {0} to populate with value {1}.", controlTypeName, value)); } }
public static string GetEnumValuesAsCsv(Type enumType) { Array enumValues = EnumHelperWindows.GetEnumValues(enumType); StringBuilder result = new StringBuilder(); int enumValuesLength = enumValues.GetLength(0); for (int i = 0; i < enumValuesLength; i++) { string enumValue = enumValues.GetValue(i).ToString(); if (i < (enumValuesLength - 1)) { result.AppendFormat("{0},", enumValue); } else { result.AppendFormat("{0}", enumValue); } } return(result.ToString()); }