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. mapred.JobClient: No job jar file set. User classes may not be found. See JobConf(Class) or JobConf#setJar(String).

    报错详情: WARN mapred.JobClient: No job jar file set.  User classes may not be found. See JobConf(Class) ...

  2. 1074 Reversing Linked List

    题意: 每k个元素反转链表,不足k个就不反转.如原链表为1→2→3→4→5→6,k=3,则反转后的链表为3→2→1→6→5→4:若k=4,则反转后的链表为4→3→2→1→5→6. 思路: 这题会比较烦 ...

  3. 第14 章 Spring MVC的工作机制与设计模式

    14.1 Spring MVC的总体设计 要使用SPring MVC,只要在web.xml中配置一个DispatcherServlet. 再定义一个dispatcherServlet-servlet. ...

  4. Metasploit自动化脚本Ezsploit

    打开文件夹并赋予权限 ┌─[root@sch01ar]─[~] └──╼ #cd /sch01ar/ezsploit/ && ls ezsploit.sh README.md ┌─[r ...

  5. Android 4 学习(12):Linkify & Broadcast event

    参考<Professional Android 4 Development> Linkify Linkfy类可以在Text View中创建超链接.匹配LInkify中正则表达式的文本将被L ...

  6. MongoDB在Java下的增删查改

    我们总不能一直使用cmd对数据库操作,数据库总是要在程序中使用的.今天来说一下怎么通过Java调用MongoDB. 学习一下最基本也是最常用的增删查改语句,这是使用数据库的基础. 注意事项: 1.要打 ...

  7. GridView导出成Excel字符"0"丢失/数字丢失的处理方式 收藏

    GridView导出成Excel字符"0"丢失/数字丢失的处理方式 收藏 GridView 导出成Excel文件,这个代码在网上比较多.但是发现存在一个问题,导出的数据中如果有&q ...

  8. Libevent使用例子,从简单到复杂

    转载请注明出处:http://blog.csdn.net/luotuo44/article/details/39670221 本文从简单到复杂,展示如何使用libevent.网上的许多例子都是只有服务 ...

  9. linux下FTP使用

    如何在linux下开启FTP服务 1. 首先服务器要安装ftp软件,查看是否已经安装ftp软件下:    #which vsftpd    如果看到有vsftpd的目录说明服务器已经安装了ftp软件 ...

  10. 继承ServletContextListener可以完成的事情

    1.定时任务: 定时任务是从某个固定的时间开始执行特定的程序,继承这个方法,可以实现刚启动项目的时候执行某特定的程序,完成给客户部署的时即可以看到某个页面的效果. 2.初始化系统常量等: 这样来完成系 ...