public static void SetValue <T>(this DataGridViewRow row, DataGridViewColumn column, T value = default(T), bool withoutValidation = false) { if (row == null) { throw new ArgumentNullException(nameof(row)); } /* Need, but slowly * var actualValue = row.Cells[columnName].Value; * if (actualValue != null && actualValue.GetType() != typeof(T)) * { * throw new ArgumentException($@"row.SetValue: Trying to install a different type. * ColumnName: {columnName} * Actual type: {row.Cells[columnName].Value.GetType()} * Value type: {typeof(T)}"); * } */ var view = ((TView)row.DataGridView); var temp = view.ValidationEnabled; if (withoutValidation) { view.ValidationEnabled = false; } column.ValueType = value.GetType(); row.GetCell(column).Value = value; if (withoutValidation) { view.ValidationEnabled = temp; } }
public static T GetValue <T>(this DataGridViewRow row, DataGridViewColumn column) { if (row == null) { throw new ArgumentNullException(nameof(row)); } var value = row.GetCell(column).Value; /* Need, but slowly * if (value.GetType() != typeof(T)) * { * throw new InvalidCastException($@"Invalid cast. * ColumnName: {columnName} * Actual type: {value.GetType()} * Cast type: {typeof(T)}"); * } */ return((T)value); }