C#:如何验证datagridview单元格?

C#:如何验证datagridview单元格?,c#,winforms,datagridview,C#,Winforms,Datagridview,我有一个datagridview,在其中我只想允许用户输入数字。但是如果用户输入错误的字母,那么我会显示错误消息。但问题是,当我单击消息框上的“确定”按钮后,我想清除特定的单元格值并允许用户输入另一个值。我如何才能做到这一点 我应该使用哪个事件或函数 提前感谢….获取行索引,并从行索引获取单元格索引。你可以改变数值,这只是一个想法,我知道这是一个老问题,但我首先要做的是限制单元格只接受数字。为此,我重写了ProcessCmdKey方法。以下是我使用的一些代码,它应该适合您,不要忘记将gridna

我有一个datagridview,在其中我只想允许用户输入数字。但是如果用户输入错误的字母,那么我会显示错误消息。但问题是,当我单击消息框上的“确定”按钮后,我想清除特定的单元格值并允许用户输入另一个值。我如何才能做到这一点

我应该使用哪个事件或函数


提前感谢….

获取行索引,并从行索引获取单元格索引。你可以改变数值,这只是一个想法,我知道这是一个老问题,但我首先要做的是限制单元格只接受数字。为此,我重写了ProcessCmdKey方法。以下是我使用的一些代码,它应该适合您,不要忘记将gridname从dgListData更改为网格的名称:

 /// <summary>
    /// The purpose of this method is two fold.  First it stops the data grid
    /// from adding a new row when the enter key is pressed during editing a cell
    /// Secondly it filters the keys for numeric characters only when the selected 
    /// cell type is of a numeric type.
    /// </summary>
    /// <param name="msg"></param>
    /// <param name="keyData"></param>
    /// <returns></returns>
    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        //Let's get fancy and detect the column type and then filter the keys as needed.
        if (!dgListData.IsCurrentCellInEditMode) return base.ProcessCmdKey(ref msg, keyData); //only go into the routine if we are in edit mode

        if (helper.IsNumericType(dgListData.CurrentCell.ValueType))
        {
            if (helper.NumericFilterKeyData(keyData)) return true; //override the key
        }

        // Check if Enter is pressed
        if (keyData != Keys.Enter) return base.ProcessCmdKey(ref msg, keyData);
        // If there isn't any selected row, do nothing

        SetFocusToNextVisibleColumn(dgListData);
        return true;
    }

    private void SetFocusToNextVisibleColumn(DataGridView dgViewTarget)
    {
        if (dgViewTarget.CurrentCell == null) return;

        Console.WriteLine(dgViewTarget.CurrentCell.Value);

        if (dgViewTarget.CurrentCell.IsInEditMode) dgViewTarget.EndEdit();

        var currentColumn = dgViewTarget.CurrentCell.ColumnIndex;
        if (currentColumn < dgViewTarget.ColumnCount) ++currentColumn;
        for (var i = currentColumn; i < dgViewTarget.ColumnCount; i++)
        {
            if (!dgViewTarget[i, dgViewTarget.CurrentRow.Index].Displayed) continue;
            dgViewTarget.CurrentCell = dgViewTarget[i, dgViewTarget.CurrentRow.Index];
            break;
        }
    }
//
///这种方法有两个目的。首先,它停止数据网格
///在编辑单元格时按enter键时添加新行
///其次,它只在选择了
///单元格类型为数字类型。
/// 
/// 
/// 
/// 
受保护的覆盖bool ProcessCmdKey(参考消息消息消息,Keys keyData)
{
//让我们想象一下,检测列类型,然后根据需要过滤键。
如果(!dgListData.IsCurrentCellInEditMode)返回base.ProcessCmdKey(ref msg,keyData);//仅当我们处于编辑模式时才进入例程
if(helper.IsNumericType(dgListData.CurrentCell.ValueType))
{
如果(helper.NumericFilterKeyData(keyData))返回true;//重写该键
}
//检查是否按下Enter键
if(keyData!=Keys.Enter)返回base.ProcessCmdKey(ref msg,keyData);
//如果没有任何选定行,则不执行任何操作
SetFocusToNextVisibleColumn(dgListData);
返回true;
}
私有void SetFocusToNextVisibleColumn(DataGridView dgViewTarget)
{
if(dgViewTarget.CurrentCell==null)返回;
Console.WriteLine(dgViewTarget.CurrentCell.Value);
如果(dgViewTarget.CurrentCell.IsInEditMode)dgViewTarget.EndEdit();
var currentColumn=dgViewTarget.CurrentCell.ColumnIndex;
if(currentColumn
我还有一个静态助手类,我在整个项目中都使用它。这里是您需要的两个例程。只需添加一个新的helper.cs类并将此代码粘贴到其中

public static class helper
{
    private static readonly KeysConverter Kc = new KeysConverter();

    /// <summary>
    /// Filters the keys for numeric entry keys
    /// </summary>
    /// <param name="keyData"></param>
    /// <returns></returns>
    public static bool NumericFilterKeyData(Keys keyData)
    {
        var keyChar = Kc.ConvertToString(keyData);
        return !char.IsDigit(keyChar[0])
               && keyData != Keys.Decimal
               && keyData != Keys.Delete
               && keyData != Keys.Tab
               && keyData != Keys.Back
               && keyData != Keys.Enter
               && keyData != Keys.OemPeriod
               && keyData != Keys.NumPad0
               && keyData != Keys.NumPad1
               && keyData != Keys.NumPad2
               && keyData != Keys.NumPad3
               && keyData != Keys.NumPad4
               && keyData != Keys.NumPad5
               && keyData != Keys.NumPad6
               && keyData != Keys.NumPad7
               && keyData != Keys.NumPad8
               && keyData != Keys.NumPad9;
    }

    /// <summary>
    /// Determines if a type is numeric.  Nullable numeric types are considered numeric.
    /// </summary>
    /// <remarks>
    /// Boolean is not considered numeric.
    /// </remarks>
    public static bool IsNumericType(Type type)
    {
        if (type == null)
        {
            return false;
        }

        switch (Type.GetTypeCode(type))
        {
            case TypeCode.Byte:
            case TypeCode.Decimal:
            case TypeCode.Double:
            case TypeCode.Int16:
            case TypeCode.Int32:
            case TypeCode.Int64:
            case TypeCode.SByte:
            case TypeCode.Single:
            case TypeCode.UInt16:
            case TypeCode.UInt32:
            case TypeCode.UInt64:
                return true;
            case TypeCode.Object:
                if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
                {
                    return IsNumericType(Nullable.GetUnderlyingType(type));
                }
                return false;
        }
        return false;
    }

    /// <summary>
    /// Tests if the Object is of a numeric type This is different than testing for NumericType
    /// as this expects an object that contains an expression like a sting "123"
    /// </summary>
    /// <param name="expression">Object to test</param>
    /// <returns>true if it is numeric</returns>
    public static bool IsNumeric(Object expression)
    {
        if (expression == null || expression is DateTime)
            return false;

        if (expression is Int16 || expression is Int32 || expression is Int64 || expression is Decimal ||
            expression is Single || expression is Double || expression is Boolean)
            return true;

        try
        {
            if (expression is string)
            {
                if (
                    expression.ToString()
                        .IndexOfAny(
                            @" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOOPQRSTUVWXYZ!@#$%^&*?<>[]|+-*/\'`~_"
                                .ToCharArray()) != -1) return false;
            }
            else
            {
                Double.Parse(expression.ToString());
            }
            return true;
        }
        catch
        {
        } // just dismiss errors but return false
        return false;
    }
}
公共静态类帮助器
{
私有静态只读密钥转换器Kc=新密钥转换器();
/// 
///筛选数字输入键的键
/// 
/// 
/// 
公共静态bool NumericFilterKeyData(Keys keyData)
{
var keyChar=Kc.ConvertToString(keyData);
return!char.IsDigit(keyChar[0])
&&keyData!=键。十进制
&&keyData!=密钥。删除
&&keyData!=Keys.Tab
&&keyData!=键。返回
&&keyData!=键。输入
&&keyData!=Keys.OemPeriod
&&keyData!=Keys.NumPad0
&&keyData!=Keys.NumPad1
&&keyData!=Keys.NumPad2
&&keyData!=Keys.NumPad3
&&keyData!=Keys.numpa4
&&keyData!=Keys.NumPad5
&&keyData!=Keys.NumPad6
&&keyData!=Keys.NumPad7
&&keyData!=Keys.NumPad8
&&keyData!=Keys.NumPad9;
}
/// 
///确定类型是否为数字。可为空的数字类型被视为数字类型。
/// 
/// 
///布尔值不被视为数字。
/// 
公共静态bool IsNumericType(类型)
{
if(type==null)
{
返回false;
}
开关(类型.GetTypeCode(类型))
{
大小写类型代码。字节:
大小写类型代码。十进制:
案例类型代码。双:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
案例类型代码.SByte:
案例类型代码。单个:
案例类型代码.UInt16:
案例类型代码.UInt32:
案例类型代码.UInt64:
返回true;
案例类型代码。对象:
if(type.IsGenericType&&type.GetGenericTypeDefinition()==typeof(可为空))
{
返回IsNumericType(null.getUnderlineType(type));
}
返回false;
}
返回false;
}
/// 
///测试对象是否为数字类型这与测试NumericType不同
///因为这需要一个包含类似sting“123”表达式的对象
/// 
///测试对象
///如果为数字,则为true
公共静态bool IsNumeric(对象表达式)
{
if(表达式==null | |表达式为DateTime)
返回false;
if(表达式为Int16 | |表达式为Int32 | |表达式为Int64 | |表达式为十进制||
表达式为单| |表达式为双| |表达式为布尔型)
返回true;
尝试
{
if(表达式为字符串)
{
如果(
表达式.ToString()
.IndexOfAny(
@“abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOOPQRSTUVWXYZ!@$%^&*?[]+-*/\'```”
.ToCharArray())!=-1)返回false;
}
其他的
{
Parse(expression.ToString());
}
返回true;
}
抓住
{
}//j