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         }
  • 相关阅读:
    EBS SQL > Form & Report
    oracle sql 优化分析点
    MRP 物料需求计划
    MRPII 制造资源计划
    Barcode128 应用实务
    Oracle SQL语句优化技术分析
    APPSQLAP10710 Online accounting could not be created. AP Invoice 无法创建会计分录
    Oracle数据完整性和锁机制
    ORACLE Responsibility Menu Reference to Other User
    EBS 常用 SQL
  • 原文地址:https://www.cnblogs.com/liuzz/p/14755427.html
Copyright © 2011-2022 走看看