如何将特定的datagridview单元格限制为仅数字? [英] How can I restrict a particular datagridview cell to number only?

查看:81
本文介绍了如何将特定的datagridview单元格限制为仅数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我,

这是我用于验证事件的桌面应用程序,但
我还使用了currentcelldirtystatechange事件.
我的问题是在检查
之前触发了当前..事件 验证单元格或数字数据.

我该怎么办?

帮忙!!!
:confused:

Please help me,

It is a desktop application i used in validating event but
I am also used currentcelldirtystatechange event.
my problem is the current.. event is fired before checking
a validate cell or number data.

What should i do?

help!!!
:confused:

推荐答案

请参见此处此处.


希望下面的代码可以帮助您限制datagridview


hope below code can help you in restricting datagridview


private void dtgView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (this.dtgView.CurrentCell.ColumnIndex == dtgView.Columns["Column2"].Index & (e.Control != null))
            {
                TextBox tb = (TextBox)e.Control;
                tb.KeyPress += TextBox_KeyPress;
            }
        }











private void TextBox_KeyPress(System.Object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            //---if textbox is empty and user pressed a decimal char---
            if (((TextBox)sender).Text == string.Empty & e.KeyChar == (char) 46)
            {
                e.Handled = true;
                return;
            }
            //---if textbox already has a decimal point---
            if (((TextBox)sender).Text.Contains(Convert.ToString((char)46)) & e.KeyChar == (char)46)
            {
                e.Handled = true;
                return;
            }
            //if (!(char.IsDigit(e.KeyChar).ToString() ==","))
            if (!(e.KeyChar == 44) & !(e.KeyChar == 45))
            {
                if ((!(char.IsDigit(e.KeyChar) | char.IsControl(e.KeyChar) | (e.KeyChar == (char)46))))
                {
                    e.Handled = true;
                }
            }
        }


您应该制作一个自定义文本框单元格,然后在datagridview中使用.这是非常好的实现 DataGridView的自定义数值编辑元素 [
You should make a custom textbox cell and then use in datagridview. here is very good implementation Custom Numeric Edit Elements for DataGridView[^]


这篇关于如何将特定的datagridview单元格限制为仅数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆