dataGridview中单元格的值只允许输入数字??? dataGridview中单元格的值只允许输入数字并且随时捕获到用户输入的每一个数字??怎么实现?? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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#自定义组件的问题 关于ocx的调用 九九乘法表中如何用tab控制位置? vs2005中的依赖选项卡这么在类设计器中没有啊 关于查询统计过程显示等待提示框的问题!!! 线程,线程,线程----------还是线程 是不是web.config的问题呢? 在FORM之间传递消息 请问又没有可以拖动的编辑框阿? 讨论一下:关于TreeView 报错: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;
}
}
}
参考