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

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 的事件中,判断单元格,所在列,调取文本输入事件

转载于:https://www.cnblogs.com/hanke123/p/5753810.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本文档不准备面面俱到地介绍DataGridView,而是着眼于深入地介绍一些技术点的高级特性。 本文档按逻辑分为5个章节,首先是结构和特性的概览,其次是内置的列/单元格类型的介绍,再次是数据操作相关的内容,然后是主要特性的综述,最后是最佳实践。 大部分章节含有一个“Q & A”部分,来回答该章节相关的一些常见问题。注意,某些问题会由于知识点的关联性重复出现在多个章节。这些问题、答案及其附带的示例代码都包含在本文档的附录部分。 一、DataGridView技术点的高级特性。 11 1 何为DataGridView 11 1.1 DataGridView和DataGrid 之间的区别 11 1.2 DataGridView的亮点 12 2 DataGridView的结构 13 2.1 结构元素(Architecture Elements) 13 2.2 单元格和组(Cells and Bands) 13 2.3 DataGridView单元格 (DataGridViewCell) 13 2.3.1 DataGridViewCell的工作机制 14 2.3.2 常见问题 15 2.4 DataGridView的列(DataGridViewColumn) 16 2.5 DataGridView的编辑控件(Editing Controls) 16 2.6 DataGridViewRow 17 2.6.1 常见问题 17 3 列/单元格类型揭密(column/cell types) 17 3.1 DataGridViewTextBoxColumn 18 3.2 DataGridViewCheckBoxColumn 19 3.3 DataGridViewImageColumn 19 3.4 DataGridViewButtonColumn 19 3.5 DataGridViewComboBoxColumn 20 3.5.1 DataError事件和ComboBox列 20 3.5.2 常见问题 20 3.6 DataGridViewLinkColumn 21 4 操作数据(Working with Data) 21 4.1 数据输入验证的相关事件 21 4.1.1 数据验证相关事件的顺序 21 4.1.2 验证数据 22 4.1.3 在新行中的数据输入(Data Entry in the New Row) 22 4.1.3.1 显示新行 22 4.1.3.2 为生成的新行添加默认值 22 4.1.3.4 在新行中输入数据 23 4.1.3.5 自定义新行的可视化效果 23 4.1.3.6 新行的排序 24 4.1.3.7 关于新行,还要注意: 24 4.1.3.8 Virtual Mode下的新行 24 4.2 关于Null值 24 4.2.1 NullValue属性 24 4.2.2 DataSourceNullValue属性 25 4.3 DataError事件 25 4.4 数据绑定模式(Databound modes) 26 4.4.1 非绑定模式(Unbound Mode) 26 4.4.2 绑定模式(Bound Mode) 26 4.4.2.1 有效的数据源 27 4.4.3 虚拟模式 27 4.4.4 混合模式 – 绑定与非绑定模式 27 4.4.5 常见问题 28 5 特性综览(Overview of features) 28 5.1 样式(Styling) 28 5.1.1 The DataGridViewCellStyle Class 29 5.1.2 Using DataGridViewCellStyle Objects 29 5.1.3 Style Inheritance 30 5.1.4 Setting Styles Dynamically 34 5.2 Custom painting 35 5.2.1 Paint Parts 35 5.3.1 在Windows窗体DataGridView控件调整大小选项 39 5.3.2 Resizing with the Mouse用鼠标调整大小 42 5.3.3 Automatic Sizing自动调整大小 43 5.3.4 Programmatic Resizing编程调整大小 45 5.3.5 Customizing Content-based Sizing Behavior自定义基于内容的调整大小行为 46 5.3.6 Content-based Sizing Options基于内容的调整大小选项 47 5.4 Selection modes选择模式 47 5.4.1 Programmatic Selection编程选择 49 5.5 滚动(Scrolling) 49 5.5.1 Scroll event Scroll事件 49 5.5.2 Scroll bars滚动条 50 5.5.3 Scrolling Properties滚动属性 50 5.6 Sorting排序 50 5.6.1 Programmatic Sorting编程排序 52 5.6.2 Custom Sorting自定义排序 53 5.6.3 Common questions and scenarios常见问题及案例 54 5.7 Border styles边框样式 55 5.7.1 Standard Border Styles标准边框样式 55 5.7.2 Advanced Border Styles高级边框风格 56 5.8 Enter-Edit modes输入,编辑模式 57 5.9 Clipboard copy modes剪贴板拷贝模式 58 5.10 Frozen columns/rows冻结的列/行 60 5.11 Implementing Custom cells and editing controls/cells实现自定义和编辑控制单元格/单元格 60 5.11.1 IDataGridViewEditingControl 接口 61 5.11.2 IDataGridViewEditingCell 接口 61 5.12 Virtual mode虚拟模式 61 5.12.1 Bound Mode and Virtual Mode绑定模式和虚拟模式 62 5.12.2 Supplementing Bound Mode补充绑定模式 62 5.12.3 Common questions and scenarios常见问题及案例 62 5.12.4 Replacing Bound Mode更换绑定模式 63 5.12.5 Virtual-Mode Events虚拟模式事件 63 5.12.6 Best Practices in Virtual Mode在虚拟模式下的最佳实践 66 5.13 容量(Capacity) 66 6 最佳实践(Best Practices) 67 6.1 Using Cell Styles Efficiently使用高效单元格样式 67 6.2 Using Shortcut Menus Efficiently使用高效快捷菜单 68 6.3 Using Automatic Resizing Efficiently使用自动调整大小高效 69 6.4 Using the Selected Cells, Rows, and Columns Collections Efficiently高效使用选定的单元格,行和列的集合 69 6.5 Using Shared Rows 使用共享行 70 6.6 Preventing Rows from Becoming Unshared 防止行成为非共享 72 附录 A – FAQ 75 1. 如何使指定的单元格不可编辑? 75 2. 如何让一个单元格不可用(disable)? 75 3. 如何避免用户将焦点设置到指定的单元格? 77 4. 如何使所有单元格总是显示控件(不论它是否处于编辑状态)? 77 5. Why does the cell text show up with “square” characters where they should be new lines(TODO,未能实现该效果)? 78 6. 如何在单元格内同时显示图标和文本? 78 7. 如何隐藏一列? 80 8. 如何避免用户对列排序? 81 9. 如何针对多个列排序? 81 9.1 将数据绑定到DataGridView时 81 9.2 Unbound DataGridView 取消绑定 82 9.2.1 Custom Sorting Using the SortCompare Event 使用排序结束时间实现用户自定义排序 82 9.2.2 Custom Sorting Using the IComparer Interface使用IComparer接口实现自定义排序 84 10. 如何为编辑控件添加事件处理函数? 86 11. 应在何时移除编辑控件的事件处理函数? 87 12. 如何处理ComboBox列中控件的SelectIndexChanged事件? 87 13. 如何通过拖放调整行的顺序? 87 14. 如何调整最后一列的宽度使其占据网格的剩余客户区? 89 15. 如何让TextBox类型单元格支持换行? 89 16. 如何使Image列不显示任何图像(字段值为null时)? 90 17. 如何能够在ComboBox类型单元格输入数据? 90 18. How do I have a combo box column display a sub set of data based upon the value of a different combo box column(TODO)? 91 19. 如何在用户编辑控件的时候(而不是在验证时)就显示错误图标? 92 20. 如何同时显示绑定数据和非绑定数据? 94 21. How do I show data that comes from two tables(TODO)?如何显示来自两个数据源的数据? 96 22. 如何显示主从表? 97 23. 如何在同一DataGridView显示主从表? 99 24. 如何避免用户对列排序? 99 25. 如何在点击工具栏按钮的时候将数据提交到数据库? 99 26. 如何在用户删除记录时显示确认对话框? 99

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值