To do this you have to make a Method Named validate(char Key) and after write code on keyPress Event
#region Decimal Validation
public static bool validate(char Key)
{
byte i;
bool parsed = byte.TryParse(Key.ToString(), out i);
bool val = true;
if (parsed)
{
val = false;
}
else if (parsed == false)
{
switch (Convert.ToInt32(Key))
{
default:
val = true;
break;
case 8:
val = false; // If Backspace, allow char.
break;
case 46:
val = false; // If Delete, allow char.
break;
}
}
return val;
}
#endregion
This is the method you have to write
Write Code on Key Press Event
void datagrigview1_KeyPress(object sender, KeyPressEventArgs e)
{
#region ValidateDecimal
e.Handled = true;
int m = e.KeyChar;
if (e.KeyChar == 46)
{
//if (dGVTrans.CurrentCell.Value != null)
{
int n =datagrigview1 .CurrentCell.EditedFormattedValue.ToString().IndexOf(".");
if (n > 0)
{
return;
}
}
}
e.Handled = validate(e.KeyChar);
}
#endregion
{
#region ValidateDecimal
if (datagrigview1.CurrentCell.OwningColumn.Name == "Quantity")
{e.Handled = true;
int m = e.KeyChar;
if (e.KeyChar == 46)
{
//if (dGVTrans.CurrentCell.Value != null)
{
int n =datagrigview1 .CurrentCell.EditedFormattedValue.ToString().IndexOf(".");
if (n > 0)
{
return;
}
}
}
e.Handled = validate(e.KeyChar);
}
#endregion
No comments:
Post a Comment