控件類型:DataGridView

控件名稱:dgvGift_Condition

裏面用到的:IsNumeric、NotePastText、RestoreText 等請參見 前一日志“TextBox中只能輸入數字”。

下面例子中 dgvGift_Condition中 第2列和第4列 只能輸入數字

private DataGridViewTextBoxEditingControl EditingControl = new DataGridViewTextBoxEditingControl();

private void dgvGift_Condition_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (this.dgvGift_Condition.CurrentCell.ColumnIndex == 1 || this.dgvGift_Condition.CurrentCell.ColumnIndex == 3)
            {
                EditingControl = (DataGridViewTextBoxEditingControl)(e.Control);
                EditingControl.KeyPress += new KeyPressEventHandler(EditingControl_KeyPress);
                EditingControl.TextChanged += new EventHandler(EditingControl_TextChanged);
            }
        }

void EditingControl_TextChanged(object sender, EventArgs e)
        {
            if (this.IsNumeric(this.EditingControl.Text, true, false))
            {
                this.NotePastText(this.EditingControl, ref PastText);
            }
            else
            {
                this.RestoreText(this.EditingControl, PastText);
            }
        }

void EditingControl_KeyPress(object sender, KeyPressEventArgs e)
        {
            this.CheckKeyIn(e, false);
        }

private void dgvGift_Condition_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            if (e.ColumnIndex == 1 || e.ColumnIndex == 3)
            {
                TextBox tb = new TextBox();
                if (this.dgvGift_Condition.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null)
                {
                    tb.Text = "";
                }
                else
                {
                    tb.Text = this.dgvGift_Condition.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                }
                this.NotePastText(tb, ref PastText);
            }
        }

private void dgvGift_Condition_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            EditingControl.KeyPress -= EditingControl_KeyPress;
            EditingControl.TextChanged -= EditingControl_TextChanged;
        }

C# DataGridView的單元格中只能輸入數字的更多相关文章

  1. android ios 只能輸入數字 不能輸入小數點的 函數 cordova

    andriod function numericsonly(ob) { var invalidChars = /[^0-9]/gi if (invalidChars.test(ob.value)) { ...

  2. Winform DataGridView列的单元格中动态添加图片和文字

    先上图在说,第二列中图片和文字的样式 1.需要重写DataGridViewTextBoxColumn,新建类TextAndImageColumn.cs using System; using Syst ...

  3. DataGrid 獲取 制定 row Col 單元格

    public static class DataGridHelper     {         /// <summary>                  /// Gets the v ...

  4. WinForm中DataGridView复制选中单元格内容解决方案

    WinForm中DataGridView鼠标选中单元格内容复制方案 1.CTR+C快捷键复制 前提:该控件ClipboardCopyMode属性设置值非Disable: 2.鼠标框选,自定义代码实现复 ...

  5. 设置DataGridView的某个单元格为ComboBox

    怎么将DataGridView的 某个单元格设为ComboBox的样式而不是整列都改变样式? 1.最简单的方法:利用DataGridView提供的DataGridViewComboBoxCell. 写 ...

  6. ASP.NET MVC 單元測試系列

    ASP.NET MVC 單元測試系列 (7):Visual Studio Unit Test 透過 Visual Studio 裡的整合開發環境 (IDE) 結合單元測試開發是再便利不過的了,在 Vi ...

  7. POI如何自动调整Excel单元格中字体的大小

    问题 目的是要将Excel中的文字全部显示出来,可以设置对齐格式为[缩小字体填充],但是这样的话只能展示出一行数据,字体会变得很小.还有一种办法,设置对齐格式为[自动换行],然后让单元格中的字体自动调 ...

  8. 控制input中只能输入固定格式内容

    onkeyup 事件会在键盘按键被松开时发生,onafterpaste 是粘贴触发,没有这个事件用onblur吧,失去焦点时发生输入完,点其他地方就会执行. <html lang="e ...

  9. 当没有用 EXISTS 引入子查询时,在选择列表中只能指定一个表达式。

    当没有用 EXISTS 引入子查询时,在选择列表中只能指定一个表达式.比如 select * from T_Employee where FNumber not in ( select top 5*  ...

随机推荐

  1. 部署mongodb中需要注意的调参

    部署mongodb的生产服务器,给出如下相关建议: 使用虚拟化环境: 系统配置 1)推荐RAID配置 RAID(Redundant Array of Independent Disk,独立磁盘冗余阵列 ...

  2. linux 改动rootpassword以及忘记rootpassword

    改动rootpassword: $ passwd root 或者sudo passwd root $password:  (要求输入旧的密码) $new  password:(输入两遍新密码) 忘记r ...

  3. EasyPlayer-RTSP播放器:从底层到上层专注于RTSP播放Windows、Android、iOS RTSP Player

    EasyPlayer-RTSP播放器是一套RTSP专用的播放器,包括有:Windows(支持IE插件,npapi插件).Android.iOS三个平台,是由EasyDSS团队开发和维护的区别于市面上大 ...

  4. 2017-2018-1 20179209《Linux内核原理与分析》第三周作业

    一.函数调用堆栈 存储程序.函数调用堆栈(高级语言起点)和中断机制是计算机工作的三大法宝.其中函数调用堆栈是本次学习的重点.先介绍一些基本的知识点: 1.ebp 在C语言中用作记录当前函数调用的基址: ...

  5. UITableViewCell的多选操作

    版权声明:本文为博主原创文章.未经博主同意不得转载,转载需加上原博客链接. https://blog.csdn.net/panyong4627/article/details/37902207 - ( ...

  6. ME01 创建货源清单function

    CALL FUNCTION 'ME_DIRECT_INPUT_SOURCE_LIST' Function module IDOC_INPUT_SRCLST FUNCTION IDOC_INPUT_SR ...

  7. ABAP excel操作 OLE 常用方法和属性

    转自 http://bstone.blog.163.com/blog/static/176820446201172834149199/#userconsent# OLE 常用方法和属性 1.ole中如 ...

  8. Machine Learning No.4: Regularization

    1. Underfit = High bias Overfit = High varience 2. Addressing overfitting: (1) reduce number of feat ...

  9. spring-boot3代码

    App.java package com.kfit; import org.springframework.boot.SpringApplication; import org.springframe ...

  10. NLP数据集大放送,再也不愁数据了!【上百个哦】

    奉上100多个按字母顺序排列的开源自然语言处理文本数据集列表(原始未结构化的文本数据),快去按图索骥下载数据自己研究吧! 数据集 Apache软件基金会公开邮件档案:截止到2011年7月11日全部公开 ...