zoukankan      html  css  js  c++  java
  • DataGridView列值值输入非法就屏蔽,例如数字列不允许输入中文

    1、先定义键盘事件

       public DataGridViewTextBoxEditingControl CellEdit = null; // 声明 一个 CellEdit 输入法控制


    1
    private void dataGridView2_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) 2 { 3 CellEdit = (DataGridViewTextBoxEditingControl)e.Control; // 赋值 4 CellEdit.SelectAll(); 5 CellEdit.KeyPress += Cells_KeyPress; // 绑定到事件 6 }

    2、输入非法就屏蔽

     1 /// <summary>
     2         /// 单元格输入键事件
     3         /// </summary>
     4         /// <param name="sender"></param>
     5         /// <param name="e"></param>
     6         private void Cells_KeyPress(object sender, KeyPressEventArgs e)
     7         {
     8             if (dataGridView2.CurrentCellAddress.X == 11 || dataGridView2.CurrentCellAddress.X == 12 || dataGridView2.CurrentCellAddress.X == 13) // 判断当前列是不是要控制的列 我是控制的索引值为2的  列(即第三列)
     9             {
    10                 if ((Convert.ToInt32(e.KeyChar) < 48 || Convert.ToInt32(e.KeyChar) > 57) && Convert.ToInt32(e.KeyChar) != 46 && Convert.ToInt32(e.KeyChar) != 8 && Convert.ToInt32(e.KeyChar) != 13)
    11                 {
    12                     e.Handled = true;  // 输入非法就屏蔽
    13                 }
    14                 else
    15                 {
    16                     if ((Convert.ToInt32(e.KeyChar) == 46))
    17                     {
    18                         e.Handled = true;
    19                     }
    20                 }
    21             }
    22         }

     3、某数字列的值校验

     1 private void dataGridView2_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
     2         {
     3             if (e.ColumnIndex == dataGridView2.Columns["price"].Index || e.ColumnIndex == dataGridView2.Columns["amount"].Index)
     4             {
     5                 dataGridView2.Rows[e.RowIndex].ErrorText = "";
     6                 decimal NewVal = 0;
     7                 if (!decimal.TryParse(e.FormattedValue.ToString(), out NewVal) || NewVal < 0)
     8                 {
     9                     e.Cancel = true;
    10                     dataGridView2.Rows[e.RowIndex].ErrorText = "价格、金额列只能输入数字";
    11                     return;
    12                 }
    13             }
    14             else if (e.ColumnIndex == dataGridView2.Columns["qty"].Index)
    15             {
    16                 dataGridView2.Rows[e.RowIndex].ErrorText = "";
    17                 decimal NewVal = 0;
    18                 if (!decimal.TryParse(e.FormattedValue.ToString(), out NewVal) || NewVal < 0)
    19                 {
    20                     e.Cancel = true;
    21                     dataGridView2.Rows[e.RowIndex].ErrorText = "数量列只能输入整型数字";
    22                     return;
    23                 }
    24             }
    25         }
  • 相关阅读:
    jdbc连接池中c3p0的配置文件的详解以及在在java中如何使用
    idea导入myeclipes项目、运行项目
    java中身份证号和的银行卡的深度校验
    springBoot的搭建使用记录
    java分模块项目在idea中使用maven打包失败(ps:maven常用到的命令)
    js获取当前页面相关信息
    mybatis使用中的记录
    tomcat服务的启动与隐藏启动(win)
    cmd命令关闭占用程序的端口
    ping端口是否开放(windows,macos,linux)
  • 原文地址:https://www.cnblogs.com/liuzz/p/14755427.html
Copyright © 2011-2022 走看看