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

方法一定义一个方法privatevoidTextBoxDec_KeyPress(objectsender,KeyPressEventArgse){if(e.KeyChar... 方法一

定义一个方法

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(),测试时发现会多次弹出提示框,求解决方案
展开
 我来答
创作者zUzVB1Sl31
2017-08-31 · 编程类资料、英语学习资料
创作者zUzVB1Sl31
采纳数:1331 获赞数:5975

向TA提问 私信TA
展开全部

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

.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);

这样才叫真正的限制。

fujie724
推荐于2017-11-26 · TA获得超过1187个赞
知道小有建树答主
回答量:154
采纳率:100%
帮助的人:276万
展开全部

你好。


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

        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);
            }
        }


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


谢谢采纳。

本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式