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 ...
随机推荐
- bind1st bind2nd的使用
STL中的函数 bind1st. bind2nd用于将一个二元算子转换成一元算子,需要两个 参数,要转换的bf和一个值v. 参考:http://blog.csdn.net/simahao/articl ...
- leetcode 之Remove Duplicates from Sorted Array(2)
描述 Follow up for ”Remove Duplicates”: What if duplicates are allowed at most twice? For exampl ...
- js实现静态页面跳转传参
最近有个项目: 存静态web服务,一个新闻页面列表出所有新闻摘要信息,然后通过点击新闻详情访问到该新闻的详情页面: 新闻展示的页面通过ajax请求接口获取到新闻的摘要信息,预计想通过id的方式访问到新 ...
- Python+Selenium 自动化实现实例-数据驱动实例
#coding=utf-8 from selenium import webdriver driver = webdriver.Firefox() driver.implicitly_wait(10) ...
- Jury Jeopardy(反向模拟)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAy8AAAI7CAIAAABqfzNeAAAgAElEQVR4nOy9e2AU1d3/f2ov07q166
- 使用亚马逊云服务器EC2做深度学习(一)申请竞价实例
这是<使用亚马逊云服务器EC2做深度学习>系列的第一篇文章. (一)申请竞价实例 (二)配置Jupyter Notebook服务器 (三)配置TensorFlow (四)配置好的系统 ...
- 【58沈剑架构系列】DB主从一致性架构优化4种方法
需求缘起 大部分互联网的业务都是“读多写少”的场景,数据库层面,读性能往往成为瓶颈.如下图:业界通常采用“一主多从,读写分离,冗余多个读库”的数据库架构来提升数据库的读性能. 这种架构的一个潜在缺点是 ...
- AC日记——[USACO5.4]奶牛的电信Telecowmunication 洛谷 P1345
[USACO5.4]奶牛的电信Telecowmunication 思路: 水题: 代码: #include <cstdio> #include <cstring> #inclu ...
- Typo: In word 拼写检查
Settings->Inspections > Spelling > Typo 评写检查,
- TestDirector自定义管理:用户配置
一.进入Customize 1.打开TD,点击TestDirector,进入登录界面,在TD登录页面右上角点击“CUSTOMIZE(自定义)”. 2.选择要登录的域和项目,输入用户帐号和密码,点击确定 ...