dataGridview中单元格的值只允许输入数字??? dataGridview中单元格的值只允许输入数字并且随时捕获到用户输入的每一个数字??怎么实现?? 解决方案 » cell_enter事件,或者cell_keypress事件,不记得那个了然后你在这个事件里面,判断列如果是要的这一列值,你就验证是否只是数字只是数字的话,可以通过,否则,,貌似有比这更好的效果以前做过,没备份,不记得了也好久没写过winform的事件了 但是我在keypress事件里面写了验证的,单元格编辑时没有触发这个事件。。??有点奇怪 有个属性Mask 选择 00000 dataGridView1_KeyPress?dataGridView1_KeyDown?dataGridView1_KeyUp?这三个我都试过了,都不行。要写到哪个事件呢? datagridview-->编辑列--》DefaultCellStyle-->Format-->Numeric试一下。 using System.Text.RegularExpressions; static private Regex r = new Regex("^[0-9]{1,}$"); //这个可以写成静态的,就不用老是构造 private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex == 3)//第四列的值为数字 { if(!r.IsMatch(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString())) { MessageBox.Show("请输入数字"); dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = ""; } } }试试 呵呵 private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { double d= 0.0; dataGridView1.Rows[e.RowIndex].ErrorText = string.Empty; if(dataGridView1.Columns[e.ColumnIndex].DataPropertyName == "") { if(!double.TryParse(e.FormattedValue.ToString(),out d) ) { dataGridView1.Rows[e.RowIndex].ErrorText = "数量输入有误!"; e.Cancel = true; } } } 参考 C# 中文分词技术有哪些? using () {...}这种结构中,包含异常处理功能吗? 在线等 这段代码的什么意思啊 UpdatePanel与FileUpload的问题 判断 一个字符串数组 是否能匹配 一个ArrayList 里的任何一个元素.Tostring() 做好设计器后,如何把组件保存到XML中并能回执 超极郁闷的SqlCommand语句... 保存时事件触发两次,有谁知道原理! 让老人笑话的简单问题,来者有分! button的image怎么通过程序添加? 报错:Exception handled in FpSpread.WndProc Image控件绑定问题,求高手
然后你在这个事件里面,判断列
如果是要的这一列值,你就验证是否只是数字
只是数字的话,可以通过,否则,,
貌似有比这更好的效果
以前做过,没备份,不记得了
也好久没写过winform的事件了
??有点奇怪
dataGridView1_KeyDown?
dataGridView1_KeyUp?
这三个我都试过了,都不行。
要写到哪个事件呢?
static private Regex r = new Regex("^[0-9]{1,}$"); //这个可以写成静态的,就不用老是构造 private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 3)//第四列的值为数字
{
if(!r.IsMatch(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()))
{
MessageBox.Show("请输入数字");
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "";
}
}
}试试 呵呵
{
double d= 0.0;
dataGridView1.Rows[e.RowIndex].ErrorText = string.Empty;
if(dataGridView1.Columns[e.ColumnIndex].DataPropertyName == "")
{
if(!double.TryParse(e.FormattedValue.ToString(),out d) )
{
dataGridView1.Rows[e.RowIndex].ErrorText = "数量输入有误!";
e.Cancel = true;
}
}
}
参考