c# DataGridView中的单元格中只能输入数字的一种解决方法
最近遇到c#的问题。那就在DataGridView中只能输入数字
最后终于找到一种方法了,那就是给DataGirdView添加CellParsing方法,代码如下:
private void dataGridView5_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
System.Text.RegularExpressions.Regex reg1 = new System.Text.RegularExpressions.Regex(@"^[-]?\d+[.]?\d*$");
if (!reg1.IsMatch(e.Value.ToString()))
{
MessageBox.Show("对不起!必须是数字,请重新输入!");
e.Value = "0.00";
e.ParsingApplied = true;
}
}
虽然现实不是很好。但还是勉强可以使用!如果那位朋友有更好的方法希望能留言给我,交流一下!
浙公网安备 33010602011771号