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. linux 下安装php扩展

    linux下安装php扩展 步骤: 1.在扩展解压包目录执行 phpize 2.执行 ./configure --with-php-config=/usr/local/php/bin/php-conf ...

  2. 好用的一个object c 宏

    好用的一个object c 宏 from https://github.com/justzt/ios-helper/blob/master/Macro.h // // Macro.h // Photo ...

  3. MFC学习(五)常见面试题

    1:应用程序类 CTestOneApp::InitInstance 可以看做是MFC程序的入口函数,main函数隐藏在这个函数中.实际开发中一般不需要对这个类进行操作,但如果要在建立主对话框之前处理一 ...

  4. Py修行路 python基础(六)前期基础整理

    计算机中,有且仅有CPU具有执行权限,能够执行指令的只有CPU! 人在应用程序上输入的文字或文本,叫做明文! 在屏幕上输入或是输出的是字符,计算机保存的是 有字符编码的 二进制数. 变量赋值规则:例如 ...

  5. get与post两种方式的优缺点

    get: get是从服务器上获取数据,post是向服务器传送数据: get传送的数据量较小,不能大于2KB.post传送的数据量较大,一般被默认为不受限制.但理论上,IIS4中最大量为80KB,IIS ...

  6. CDM中,创建一个或多个组合属性的唯一约束

    除主键外,有时还需要创建一个或多个组合字段的唯一约束,方法如下: 双击打开实体,在idntifier标签页中可看到默认主键的唯一约束,在其下方添加一条记录,然后双击该记录,打开约束设置窗口 在该窗口的 ...

  7. MySQL: [Err] 1093 - You can't specify target table 'bk' for update in FROM clause

    错误的意思说,不能先select出同一表中的某些值,再update这个表(在同一语句中). 例如下面这个sql: delete from tbl where id in (        select ...

  8. ubuntu12 安装redis和phpRedisAdmin详细流程

    一.Ubuntu安装redis(redis默认端口6379) 方式一.直接下载源码,编译(redis可以编译源码之后直接运行,不需要安装) 1.1执行命令,从官网下载源码编译: $ wget http ...

  9. VS2010中将CString转换为const char*

    最近碰到了CString 转 const char *的问题. 以前只要简单的一个强制转换就OK了,可现在是不行了,搜索了很多资料,终于搞定,主要是Unicode和ANSI的问题,只要做一个转换就可以 ...

  10. python:if 语句的使用方法

    if-else类型: #if-else num = int(input("输入成绩!")) if num > 60: print ("及格") else: ...