1.只能输入double类型

  private void textBoxX6_KeyPress(object sender, KeyPressEventArgs e)
{
{
//数字0~9所对应的keychar为48~57,小数点是46,Backspace是8
e.Handled = true;
//输入0-9和Backspace del 有效
if ((e.KeyChar >= 47 && e.KeyChar <= 58) || e.KeyChar == 8)
{
e.Handled = false;
}
if (e.KeyChar == 46) //小数点
{
if (textBoxX6.Text.Length <= 0)
e.Handled = true; //小数点不能在第一位
else
{
float f;
if (float.TryParse(textBoxX6.Text + e.KeyChar.ToString(), out f))
{
e.Handled = false;
}
}
}
}

  2.只能输入数字

private void textbox1_KeyPress(object sender, KeyPressEventArgs e)
{
  // 允许输入:数字、退格键(8)、全选(1)、复制(3)、粘贴(22)
  if (!Char.IsDigit(e.KeyChar) && e.KeyChar != 8 &&
  e.KeyChar != 1 && e.KeyChar != 3 && e.KeyChar != 22)
  {
    e.Handled = true;
  }
}

  3.

ESC  27           7  55
SPACE 32 8 56
! 33 9 57
" 34 : 58
# 35 ; 59
$ 36 < 60
% 37 = 61
& 38 > 62
' 39 ? 63
( 40 @ 64
) 41 A 65
* 42 B 66
+ 43 C 67
' 44 D 68
- 45 E 69
. 46 F 70
/ 47 G 71
0 48 H 72
1 49 I 73
2 50 J 74
3 51 K 75
4 52 L 76
5 53 M 77
6 54 N 78
O 79 g 103
P 80 h 104
Q 81 i 105
R 82 j 106
S 83 k 107
T 84 l 108
U 85 m 109
V 86 n 110
W 87 o 111
X 88 p 112
Y 89 q 113
Z 90 r 114
[ 91 s 115
\ 92 t 116
] 93 u 117
^ 94 v 118
_ 95 w 119
` 96 x 120
a 97 y 121
b 98 z 122
c 99 { 123
d 100 | 124
e 101 } 125
f 102 ~ 126

  

75=<e.char<=122 为字符

48=<e.char<=56 为字符

2.单元格输入验证

   this.dGV.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dGV_EditingControlShowing);
}
void dGV_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (this.dGV.CurrentCell.ColumnIndex == 4)
{
e.Control.KeyPress -= new KeyPressEventHandler(TextBoxDec_KeyPress);
e.Control.KeyPress += new KeyPressEventHandler(TextBoxDec_KeyPress);
}
} private void TextBoxDec_KeyPress(object sender, KeyPressEventArgs e)
{
if (this.dGV.CurrentCell.ColumnIndex ==4)
{
if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar) && e.KeyChar != '.')
{
e.Handled = true;
}
} }

  要点:在 EditingControlShowing 的事件中,判断单元格,所在列,调取文本输入事件

C# 文本输入限制类型,datagridview单元格输入验证的更多相关文章

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

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

  2. WinForm中DataGridView验证单元格输入的是数字

    转载:http://www.cnblogs.com/ganqiyin/archive/2013/02/18/2915491.html 事件:DataGridView验证单元格输入的是数字,DataGr ...

  3. Winform Datagridview 单元格html格式化支持富文本

    Winform Datagridview 单元格html格式化支持富文本 示例: 源码:https://github.com/OceanAirdrop/DataGridViewHTMLCell 参考: ...

  4. 设置DataGridView单元格的文本对齐方式

    实现效果: 知识运用: DataGridViewCellStyle类的Alignment属性     //获取或设置DataGridView单元格内的单元格内容的位置 public DataGridV ...

  5. DataGridView 单元格自动填充

    在DataGridView单元格中,当输入指定字符时,自动完成填充. 通过 TextBox实现 AutoCompleteMode AutoCompleteMode.Suggest: AutoCompl ...

  6. 【Winform-自定义控件】DataGridView 单元格合并和二维表头

    DataGridView单元格合并和二维表头应用: //DataGridView绑定数据 DataTable dt = new DataTable(); dt.Columns.Add("); ...

  7. WinForm笔记1:TextBox编辑时和DataGridView 单元格编辑时 的事件及其顺序

    TextBox 编辑框 When you change the focus by using the mouse or by calling the Focus method, focus event ...

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

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

  9. DataGridView单元格合并

    本文章转载:http://www.cnblogs.com/xiaofengfeng/p/3382094.html 图: 代码就是如此简单 文件下载:DataGridView单元格合并源码 也可以参考: ...

随机推荐

  1. Data_Structure-绪论作业

    一.作业题目 仿照三元组或复数的抽象数据类型写出有理数抽象数据类型的描述 (有理数是其分子.分母均为整数且分母不为零的分数). 有理数基本运算: 构造有理数T,元素e1,e2分别被赋以分子.分母值 销 ...

  2. Netty实现原理浅析

    1.总体结构 先放上一张漂亮的Netty总体结构图,下面的内容也主要围绕该图上的一些核心功能做分析,但对如Container Integration及Security Support等高级可选功能,本 ...

  3. catkin 工作空间 - Package 组成

    package 是 ROS 软件的基本组织形式,ROS 就是由一个个的 package 组成的 package 是 catkin 的编译基本单元 一个 package 可以包含多个可执行文件(节点) ...

  4. 阿里云ECS centos7 支持IPv6

    1.编辑 /etc/sysctl.conf 文件,将其中三条禁用IPv6的设置更改为: net.ipv6.conf.all.disable_ipv6 = 0 net.ipv6.conf.default ...

  5. Centos 6.* / 7.*下通过yum安装php7

    查看 centos 版本 # cat /etc/centos-release 删除之前的 php 版本 # yum remove php* php-common rpm 安装 Php7 相应的 yum ...

  6. krpano之字幕添加

    字幕是指介绍语音的字幕,字幕随着语音的播放而滚动,随语音暂停而暂停.字幕添加的前提是用之前的方法添加过介绍语音. 原理: 字幕层在溢出隐藏的父元素中向右滑动,当点击声音控制按钮时,字幕位置被固定,再次 ...

  7. C++中的explicit关键字 - 抑制隐式转换(转)

    在C++程序中很少有人去使用 explicit 关键字,不可否认,在平时的实践中确实很少能用的上.再说C++的功能强大,往往一个问题可以利用好几种C++特性去解决.但稍微留心一下就会发现现有的MFC库 ...

  8. Oracle行转列LISTAGG函数

    工作过程中需要将查询的数据分组并显示在一行.以往的工作经验,在sql server中可以用for xml path来实现. 现提供Oracle数据库的行转列方式 oracle11g官方文档简介如下: ...

  9. java基础之集合长度可变的实现原理

    首先我们要明白java中的集合Collection,List,ArrayList之间的关系: ArrayList是具体的实现类,实现了List接口 List是接口,继承了Collection接口 Li ...

  10. jQuery简单介绍及基本用法(选择器&DOM操作)

    简介 jQuery是一个快速.简洁的JavaScript框架,是继Prototype之后又一个优秀的JavaScript代码库(或JavaScript框架).jQuery设计的宗旨是“write Le ...