private void Cells_keypress (object sender, KeyPressEventArgs e)//custom Event
{
if (this.datagridview1.currentcelladdress.x = 2)
{
string AA = @ "^\d+$";
string bb = @ "^\. {0,1}$ ";
Regex reg1 = new regex (AA);
Regex reg2 = new Regex (BB);
if (!REG1. IsMatch (E.keychar.tostring ()) &&!REG2. IsMatch (E.keychar.tostring ()) e.handled = true;
string cc = @ "^\d+ (\.{ 0,1}\d{0,1}) $ ";//@" ^ (-?\d+) (\.\d+) $ ";
Char[] point={'. '};
String[] Seperated=celledit.text.split (point);
if (seperated. LENGTH>1)//The string after the decimal point
{
if (seperated[1). Length >= 2//.22, the number of digits after the decimal point exceeds 2
&& (E.keychar >= ' 0 ' && e.keychar <= ' 9 ')//At the same time, enter a number after the point
&& celledit.selectionlength <= 0///No number currently entered is selected
&& celledit.selectionstart>=celledit.text.length-1//cursor not at the end
)
CC = @ "^\d+ (\.{ 0,1}\d{0,1}) $ ";
Else
CC = @ "^\d+ (\.{ 0,1}\d{0,2}) $ ";
}
else//If number has no decimal point, zero
CC = @ "^\d+ (\.{ 0,1}\d{0,1}) $ ";
Regex reg3 = new Regex (cc);
Regex reg = new Regex ("^\\d*\\.{ 0,1}\\d{0,2}$ ");//regex (" ^[0-9]{1} "([0-9]|[.]) $");" ^"
if (!reg3. IsMatch (celledit.text) && celledit.text!= "0") | | (E.keychar = = '. ' && CellEdit.Text.Contains ('. '))
e.handled = true;//if (!) ( E.keychar >= ' 0 ' && e.keychar <= ' 9 ')
if (E.keychar = = ' \b ') e.handled = false;
}
}