winform设置datagridview某一列只能输入数字

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了winform设置datagridview某一列只能输入数字相关的知识,希望对你有一定的参考价值。

方法一

定义一个方法

private void TextBoxDec_KeyPress(object sender, KeyPressEventArgs e)

if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar) && e.KeyChar != '.')

e.Handled = true;


然后在dataGridView的EditingControlShowing事件中
private void dataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)

if (this.dgvFenl.CurrentCell.ColumnIndex == 列索引)

e.Control.KeyPress += new KeyPressEventHandler(TextBox_KeyPress);



网上的一个方法 这个可以实现控制只能输入数字了,但是我想在控制只能输入数字的同时给用户一个提示“只能输入数字”,尝试在e.Handled = true;后面加了个messbox.show(),测试时发现会多次弹出提示框,求解决方案

那我问你们,限制只能输入 .和数字,算不算限制输入数字,下面这些算是数字吗??

.52

5868.52.544.26

795.

.

算吗??

        /// <summary>
        /// 设置数字单元格录入规则
        /// </summary>
        /// <param name="dv">表格容器</param>
        /// <param name="ik">第几列</param>
        /// <param name="bi">限制小数点位数</param>
        public void format_column(DataGridView dv,int ik,int bi)
        
            #region 设置单元格指定列只允许输入数字和点
            //编辑的时候禁止输入非法字符
            dv.EditingControlShowing += (object sender, DataGridViewEditingControlShowingEventArgs e) => 
                if (dv.CurrentCell.ColumnIndex == ik-1)
                
                    //获取单元格实例并选中
                    DataGridViewTextBoxEditingControl CellEdit = (DataGridViewTextBoxEditingControl)e.Control;
                    CellEdit.SelectAll();
                    //指定单元格按键输入控制
                    CellEdit.KeyPress += (object sen, KeyPressEventArgs ek) =>
                    
                        //当活动单元格在第ik列index为ik-1
                        if (dv.CurrentCell.ColumnIndex == ik-1)
                        
                            if (ek.KeyChar != '\\b')
                            
                                //字符不等于0~9和点、退格的时候执行以下动作
                                if (!(ek.KeyChar >= '0' && ek.KeyChar <= '9') && ek.KeyChar != '.')
                                
                                    ek.Handled = true;
                                
                                //字符是0~9或者点、或者空格
                                else
                                
                                    //获取编辑未结束的内容
                                    string alam = pm.toStr(dv.CurrentCell.EditedFormattedValue);
                                    //统计编辑状态文本小数点个数
                                    int jar = alam.Replace(".", "..").Length - alam.Length;
                                    //当输入字符是点的时候
                                    if (ek.KeyChar == '.')
                                    
                                        //单元格没有编辑过第一个禁止输入点
                                        if (alam == "")
                                        
                                            ek.Handled = true;
                                        
                                        //单元格已经编辑过情况下
                                        else
                                        
                                            //小数点只能打一个,而且要在第一个数后面
                                            if (jar > 0)
                                            
                                                ek.Handled = true;
                                            
                                            else
                                            
                                                ek.Handled = false;
                                            
                                        
                                    
                                    //当输入字符是0~9、退格的时候
                                    else
                                    
                                        if (jar > 0)
                                        
                                            //限制小数位数
                                            if (alam.Length - alam.IndexOf('.') <= bi)
                                            
                                                ek.Handled = false;
                                            
                                            else
                                            
                                                ek.Handled = true;
                                            
                                        
                                        else
                                        
                                            ek.Handled = false;
                                        
                                    
                                
                            
                            else
                            
                                ek.Handled = false;
                            
                        
                        else
                        
                            return;
                        
                    ;
                    //粘贴字符串当不是数字就为0,超过三位小数四舍五入
                    CellEdit.KeyUp += (object sen, KeyEventArgs ek) => 
                        if(ek.KeyData==(Keys.Control|Keys.V))
                        
                            if (Clipboard.ContainsText())
                            
                                for (int i = 0; i < pm.colist().Count - 1; i++)
                                
                                    if (pm.IsFloat(pm.colist()[i]) == true)
                                    
                                        dv.Rows[dv.CurrentCell.RowIndex + i].Cells[ik - 1].Value = Convert.ToDouble(pm.colist()[i]).ToString("0.###");
                                    
                                    else
                                    
                                        dv.Rows[dv.CurrentCell.RowIndex + i].Cells[ik - 1].Value = "0";
                                    
                                
                            
                        
                    ;
                
                else
                
                    return;
                
            ;
            //编辑结束消除打在最后没用的小数点
            dv.CellEndEdit += (object aed, DataGridViewCellEventArgs ed) =>
              
                 if(pm.toStr(dv.CurrentCell.EditedFormattedValue)!="")
                  
                      if(dv.CurrentCell.EditedFormattedValue.ToString().Substring(dv.CurrentCell.EditedFormattedValue.ToString().Length-1,1)==".")
                          dv.CurrentCell.Value= dv.CurrentCell.EditedFormattedValue.ToString().Substring(0,dv.CurrentCell.EditedFormattedValue.ToString().Length - 1);
                  
              ;
            #endregion
        

使用他:

        //实例化
        类 ts = new 类();
        //给表格初始化,限制第四列,允许三位小数
        ts.format_column(dataGridview表格控件, 4,3);

这样才叫真正的限制。

参考技术A

你好。


出现多次是因为在你反复进入同一个单元格之后。事件进行了重复的绑定。只需要帮上面的代码修改如下即可。

        private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        
            if (this.dgvFenl.CurrentCell.ColumnIndex == 0)
            
                e.Control.KeyPress -= new KeyPressEventHandler(TextBox_KeyPress);
                e.Control.KeyPress += new KeyPressEventHandler(TextBox_KeyPress);
            
        


在绑定事件之前先进行移除。


谢谢采纳。

本回答被提问者采纳

winform中DataGridView 列隐藏时会触发啥事件?

winform中DataGridView 列隐藏时会触发什么事件?
DataGridView中如果通过代码将某一列隐藏时,会触发什么事件?并且这个捕获这个事件时,能获取到隐藏的是哪一列

参考技术A 重新绑定时,先将 datagridview 设置为 禁止自动增长列,再绑定 数据列。datagridview默认自动增长列,如果 原 datagridview 里有 动态绑定的列,后绑定刷新会自动加入新列,因此这个BUG就产生了。

dataGridViewX2.AutoGenerateColumns = false; //不允许自动创建列,这一句。先不允许创建列,再绑定数据。
dataGridViewX2.DataSource = bindingSource1;//绑定数据源

dataGridViewX2.Columns["用户账号"].DataPropertyName = datatable1.Columns["USERS"].ToString(); //绑定数据列追问

答非所问

以上是关于winform设置datagridview某一列只能输入数字的主要内容,如果未能解决你的问题,请参考以下文章

winform中DataGridView 列隐藏时会触发啥事件?

winform datagridview 设置交叉色后,再改变某列背景色问题

数据库 C# datagridview可不可以让某一列禁止被更改

C# winform DataGridView控件DataSource 绑定一个表后修改cell值的问题

c# winform下如何实现datagridview拆分单元格或者跨行显示

如何获得DataGridView中某一列中全部的值?