转载: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验证单元格输入的是数字的更多相关文章

  1. C# Winform 中DataGridView 实现单元格输入下拉框功能

    https://blog.csdn.net/ad13adsa/article/details/82108969 private void dataGridViewX1_EditingControlSh ...

  2. winform中dataGridView单元格根据值设置新值,彻底解决绑定后数据类型转换的困难

    // winform中dataGridView单元格在数据绑定后,数据类型更改困难,只能迂回实现.有时候需要将数字变换为不同的文字描述,就会出现int32到string类型转换的异常,借助CellFo ...

  3. C# 文本输入限制类型,datagridview单元格输入验证

    1.只能输入double类型 private void textBoxX6_KeyPress(object sender, KeyPressEventArgs e) { { //数字0~9所对应的ke ...

  4. WinForm中DataGridView复制选中单元格内容解决方案

    WinForm中DataGridView鼠标选中单元格内容复制方案 1.CTR+C快捷键复制 前提:该控件ClipboardCopyMode属性设置值非Disable: 2.鼠标框选,自定义代码实现复 ...

  5. winform的datagridview单元格输入限制和右键单击datagridview单元格焦点跟着改变

    在datagridview的EditingControlShowing事件里面添加代码: if (this.dgv_pch.Columns[dgv_pch.CurrentCell.ColumnInde ...

  6. (很难啊)如何实时获取DBGrid 中当前单元格输入的内容? [问题点数:100分,结帖人yifawu100]

    如何获取DBGrid 中当前单元格输入的内容? 还没输入完成,我想实时获取 Cell中的内容,以便作其他处理,用什么事件呢? 所以Field的Onchange事件是没用的. DBGrid1.Selec ...

  7. 如何实时获取DBGrid 中当前单元格输入的内容?

    如何获取DBGrid 中当前单元格输入的内容? 还没输入完成,我想实时获取 Cell中的内容,以便作其他处理, 用什么事件呢? 所以Field的Onchange事件是没用的. 这个问题简单啊,每输入1 ...

  8. C# Winform中DataGridView的DataGridViewCheckBoxColumn使用方法

    下面介绍Winform中DataGridView的DataGridViewCheckBoxColumn使用方法: DataGridViewCheckBoxColumn CheckBox是否选中 在判断 ...

  9. C#winform中DataGridView常用的属性

    1.AllowUserToAddRows属性:指示是否向用户显示添加行的选项 AllowUserToOrderColumns属性:指示是否允许通过手动对列重新定位 AllowUserToResizeC ...

随机推荐

  1. Linux阵列 RAID详解 (转)

    原文链接:http://molinux.blog.51cto.com/2536040/516008   一. RAID详解   二. mdadm工具介绍   三. 创建一个RAID的基本过程   四. ...

  2. Metro应用Json数据处理

    Windows Phone 8 或者 Windows 8 平台对JSON数据的处理方式基本是一致的,需要使用DataContractJsonSerializer类将对象的实例序列化为JSON字符串,并 ...

  3. Jinja2 及 render_template 的深度用法

    是时候开始写个前端了,Flask中默认的模板语言是Jinja2 现在我们来一步一步的学习一下 Jinja2 捎带手把 render_template 中留下的疑问解决一下 首先我们要在后端定义几个字符 ...

  4. mysql的两种存储引擎

    MySQL 有多种存储引擎,目前常用的是 MyISAM 和 InnoDB 这两个引擎,除了这两个引擎以为还有许多其他引擎,有官方的,也有一些公司自己研发的.这篇文章主要简单概述一下常用常见的 MySQ ...

  5. Nginx 虚拟目录和虚拟主机的配置

    nginx.conf 配置文件的几个常用命令 nginx 配置文件主要分为六个区域: main: 全局设置 events: nginx工作模式 http: http设置 sever: 主机设置 loc ...

  6. Adminimize 插件:WordPress根据用户角色显示/隐藏某些后台功能

    倡萌刚才分享了 WordPress根据用户角色隐藏文章/页面的功能模块(Meta Boxes),如果你还想根据不同用户角色显示或隐藏后台的某些功能,比如 顶部工具条.左边导航菜单.小工具.仪表盘.菜单 ...

  7. 牛客练习赛19 C-托米航空公司

    思路:轮廓线dp,找bug找死我了. #include<bits/stdc++.h> #define LL long long #define fi first #define se se ...

  8. css特效-一道闪光在图片上划过

    在百度音乐 http://music.baidu.com/ 看到这么一个图片效果,当鼠标移上去的时候,会有一道闪光在图片上划过,效果挺酷炫的.于是把这个效果再实现一下:大体思想是,设计一个透明层i,s ...

  9. Python学习之pillow库入门

    http://python.jobbole.com/84956/ 我还是搬运工......

  10. 关于urllib、urllib2爬虫伪装的总结

    站在网站管理的角度,如果在同一时间段,大家全部利用爬虫程序对自己的网站进行爬取操作,那么这网站服务器能不能承受这种负荷?肯定不能啊,如果严重超负荷则会时服务器宕机(死机)的,对于一些商业型的网站,宕机 ...