控件類型: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. 一些编译php时的configure 参数

    一些编译php时的configure 参数 ./configure –prefix=/usr/local/php php 安装目录 –with-apxs2=/usr/local/apache/bin/ ...

  2. C#中的let字句应用示例

    一.应用场景 在查询表达式中,存储子表达式的结果有时很有用,这样可以在随后的子句中使用. 可以使用 let 关键字完成这一工作,该关键字可以创建一个新的范围变量,并且用您提供的表达式的结果初始化该变量 ...

  3. zookeeper启动流程简单梳理

    等着測试童鞋完工,顺便里了下zookeeper的启动流程 zk3.4.6 启动脚本里面 nohup "$JAVA" "-Dzookeeper.log.dir=${ZOO_ ...

  4. CGI的基本原理

    一.基本原理 CGI:通用网关接口(Common Gateway Interface)是一个Webserver主机提供信息服务的标准接口.通过CGI接口,Webserver就行获取client提交的信 ...

  5. python 基础2.5 循环中continue与breake用法

    示例1: #循环退出,break continue.break 跳出最外层循环:continue跳出内层循环 #当 i=5时,通过continue 跳出当前if循环,不在执行if循环中后边的语句.i= ...

  6. 1930: [Shoi2003]pacman 吃豆豆

    1930: [Shoi2003]pacman 吃豆豆 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1969  Solved: 461[Submit][ ...

  7. Consumer Group Example

    面向kafka编程 Consumer Group Example https://cwiki.apache.org/confluence/display/KAFKA/Consumer+Group+Ex ...

  8. VM tools安装错误The path "" is not a valid path to the xx generic kernel headers.

    VMWARE TOOLS安装提示THE PATH IS NOT A VALID PATH TO THE GENERIC KERNEL HEADERSI solved this problem, I g ...

  9. abap 打开文件对话框

    [转自 http://blog.csdn.net/zhongguomao/article/details/6712576] *----------------------- Method 1 ---- ...

  10. 在maven 中部署SSM项目找不 Spring ContextLoaderListener 的解决办法

    1.项目使用技术:maven的项目使用了Spring MVC+Spring +Mybatis+Tomcat搭建一个项目. 2.报错信息: Error configuring application l ...