• 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         }
  • 相关阅读:
    hdu
    《Linux命令行与shell脚本编程大全》 第十四章 学习笔记
    zoj 3665 Yukari's Birthday(枚举+二分)
    ActiveMQ使用STOMP协议的一个错误问题:Unexpected ACK received for message-id
    Ubuntu下屏幕录像、后期处理不完全攻略
    find-all-numbers-disappeared-in-an-array
    find-right-interval
    non-overlapping-intervals
    cut命令如何截取以空格隔开的字段
    arranging-coins
  • 原文地址:https://www.cnblogs.com/liuzz/p/14755427.html
Copyright © 2020-2023  润新知