WinForm中DataGridView验证单元格输入的是数字
转载:http://www.cnblogs.com/ganqiyin/archive/2013/02/18/2915491.html
事件:DataGridView验证单元格输入的是数字,DataGridView源数据是从数据库读取的。
需求:当用户输入的不是数字的时候需要提示信息(数据是直接绑定数据库的,因此dataGridView有自己的报错功能,我们需要屏蔽掉它,显示自己的错误提示!)
实现: 选择DataGridView的CellValidating事件
(1) 验证整数:
1 private void gridPlant_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
2 {
3 if (e.RowIndex > -1 && e.ColumnIndex > -1)
4 {
5 DataGridView grid = (DataGridView)sender;
6 grid.Rows[e.RowIndex].ErrorText = "";
7 //这里最好用列名,而不是列索引号做判断
8 if (grid.Columns[e.ColumnIndex].Name == "WO0011_NUMBER_IDLE" || grid.Columns[e.ColumnIndex].Name == "WO0011_NUMBER_WORKING" || grid.Columns[e.ColumnIndex].Name == "WO0011_NUMBER_ON_SITE")
9 {
10
11 Int32 newInteger = 0;
12 if (!int.TryParse(e.FormattedValue.ToString(), out newInteger))
13 {
14 e.Cancel = true;
15 grid.Rows[e.RowIndex].ErrorText = "Please enter a int number!";
16 MessageBox.Show("the value is not nubmer , Pleaser enter a int number !");
17 return;
18 }
19 }
20 }
21 }
(2) 验证十进制数:
1 private void gridBriefsOlder_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
2 {
3 if (e.RowIndex > -1 && e.ColumnIndex > -1)
4 {
5 DataGridView grid = (DataGridView)sender;
6 grid.Rows[e.RowIndex].ErrorText = "";
7
8 if (grid.Columns[e.ColumnIndex].Name == "WO0009_APPROXIMATE_COMPLETION_PERCENTAGE1")
9 {
10 try
11 {
12 Convert.ToDecimal(e.FormattedValue);
13 }
14 catch
15 {
16 e.Cancel = true;
17 grid.Rows[e.RowIndex].ErrorText = "Please enter a number!";
18 MessageBox.Show("the value is not nubmer , Pleaser enter a number !");
19 return;
20 }
21 }
22 }
23 }
//=>不设置CausesValidation话,则datagridview中CellValidating中出现无限循环了。
this.dgvRecyclePackage.CausesValidation = false;
这样就可以了。
WinForm中DataGridView验证单元格输入的是数字的更多相关文章
- C# Winform 中DataGridView 实现单元格输入下拉框功能
https://blog.csdn.net/ad13adsa/article/details/82108969 private void dataGridViewX1_EditingControlSh ...
- winform中dataGridView单元格根据值设置新值,彻底解决绑定后数据类型转换的困难
// winform中dataGridView单元格在数据绑定后,数据类型更改困难,只能迂回实现.有时候需要将数字变换为不同的文字描述,就会出现int32到string类型转换的异常,借助CellFo ...
- C# 文本输入限制类型,datagridview单元格输入验证
1.只能输入double类型 private void textBoxX6_KeyPress(object sender, KeyPressEventArgs e) { { //数字0~9所对应的ke ...
- WinForm中DataGridView复制选中单元格内容解决方案
WinForm中DataGridView鼠标选中单元格内容复制方案 1.CTR+C快捷键复制 前提:该控件ClipboardCopyMode属性设置值非Disable: 2.鼠标框选,自定义代码实现复 ...
- winform的datagridview单元格输入限制和右键单击datagridview单元格焦点跟着改变
在datagridview的EditingControlShowing事件里面添加代码: if (this.dgv_pch.Columns[dgv_pch.CurrentCell.ColumnInde ...
- (很难啊)如何实时获取DBGrid 中当前单元格输入的内容? [问题点数:100分,结帖人yifawu100]
如何获取DBGrid 中当前单元格输入的内容? 还没输入完成,我想实时获取 Cell中的内容,以便作其他处理,用什么事件呢? 所以Field的Onchange事件是没用的. DBGrid1.Selec ...
- 如何实时获取DBGrid 中当前单元格输入的内容?
如何获取DBGrid 中当前单元格输入的内容? 还没输入完成,我想实时获取 Cell中的内容,以便作其他处理, 用什么事件呢? 所以Field的Onchange事件是没用的. 这个问题简单啊,每输入1 ...
- C# Winform中DataGridView的DataGridViewCheckBoxColumn使用方法
下面介绍Winform中DataGridView的DataGridViewCheckBoxColumn使用方法: DataGridViewCheckBoxColumn CheckBox是否选中 在判断 ...
- C#winform中DataGridView常用的属性
1.AllowUserToAddRows属性:指示是否向用户显示添加行的选项 AllowUserToOrderColumns属性:指示是否允许通过手动对列重新定位 AllowUserToResizeC ...
随机推荐
- 20180615 wdcp 域名解析问题
/usr/local/nginx_bak/conf/nginx.conf 这里有个nginx的配置,修改为nginx_bak
- word2vec原理
最原始的是NNLM,然后对其改进,有了后面的层次softmax和skip gram 层次softmax:去掉了隐藏层,后面加了huffuman树,concat的映射层也变成了sum skip gram ...
- Linux自身安全SElinux
查看SELinux状态: 1./usr/sbin/sestatus -v ##如果SELinux status参数为enabled即为开启状态 SELinux status: ...
- C语言 五子棋2
#include<windows.h> #include<stdlib.h> #include<stdio.h> #include<conio.h> # ...
- js中的cookie使用和vue-cookie的使用
在HTTP协议的定义中,采用了一种机制来记录客户端和服务器端交互的信息,这种机制被称为cookie,cookie规范定义了服务器和客户端交互信息的格式.生存期.使用范围.安全性. 在JavaScrip ...
- linux中$的各种含义
我们先写一个简单的脚本,执行以后再解释各个变量的意义 # touch variable # vi variable 脚本内容如下: #!/bin/sh echo "number: ...
- Linux 基础——关机重启命令shutdown、reboot等
一.关机重启命令的作用 相信对于接触过电脑的人来说,特别是对于windows系统来说,如果长时间使用不经重启的话会出现一点点卡顿的感觉.但是当重启整个系统后,这点点卡顿的感觉好像又没了,重启后wind ...
- 【hdoj_1133】Buy the Ticket(卡特兰数+大数)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1133 题目的意思是,m个人只有50元钱,n个人只有100元整钱,票价50元/人.现在售票厅没钱,只有50元 ...
- Win + Linux下Source Insight 替代方案
需要阅读大量的系统源码,还有开源框架和好的源码等,需要寻求轻量,多快好省的editor, 最好有markdown语法的支持,支持重构,跳转调用,定义等... 自动补全... 目前看来,windows上 ...
- 如何使用Inno Setup Compiler制作安装软件包
工具/原料 Inno Setup Compiler汉化版软件 方法/步骤 启动Inno Setup Compiler汉化版软件. 选择创建新的空白脚本文件,按确定. 然后按下一步. ...