1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
class DataGridViewTextBoxNumberColumn : DataGridViewColumn
{
public DataGridViewTextBoxNumberColumn()
: base(new DataGridViewTextBoxNumberCell())
{
}
public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
// Ensure that the cell used for the template is a correct Type of Cell.
if (value != null &&
!value.GetType().IsAssignableFrom(typeof(DataGridViewTextBoxNumberCell)))
{
throw new InvalidCastException("Must be a DataGridViewTextBoxNumberCell");
}
base.CellTemplate = value;
}
}
private DataGridViewTextBoxCell TextBoxCellTemplate
{
get
{
return (DataGridViewTextBoxNumberCell)this.CellTemplate;
}
}
}
class DataGridViewTextBoxNumberCell : DataGridViewTextBoxCell
{
public DataGridViewTextBoxNumberCell()
: base()
{
this.Style.NullValue = string.Empty;
}
public override object Clone()
{
DataGridViewTextBoxNumberCell cell = base.Clone() as DataGridViewTextBoxNumberCell;
return cell;
}
// Ajout du contrôle séparateur décimal
public override object ParseFormattedValue(object formattedValue, DataGridViewCellStyle cellStyle, System.ComponentModel.TypeConverter formattedValueTypeConverter, System.ComponentModel.TypeConverter valueTypeConverter)
{
string strSep = CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
bool boolAccept = false;
if (strSep.Length > 0)
{
if (Convert.ToString(formattedValue).Contains(".") == true || Convert.ToString(formattedValue).Contains(",") == true)
{
if (Convert.ToString(formattedValue).Contains(strSep) == false)
{
if (strSep == ",")
{
formattedValue = Convert.ToString(formattedValue).Replace(".", strSep);
}
else if (strSep == ".")
{
formattedValue = Convert.ToString(formattedValue).Replace(",", strSep);
}
}
}
}
return base.ParseFormattedValue(formattedValue, cellStyle, formattedValueTypeConverter, valueTypeConverter);
}
} |
Partager