0

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

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

提前致谢....

4

2 回答 2

1

获取行索引,并从行索引中获取单元格索引。像这样你可以改变价值

于 2012-07-03T11:07:39.847 回答
0

只是一个想法,我知道这是一个老问题,但我所做的是首先将单元格限制为仅接受数字。为此,我重写了 ProcessCmdKey 方法。这是我使用的一些代码,它应该适合您,不要忘记将网格名称从 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;
        }
    }

我还有一个静态帮助程序类,我在整个项目中都使用它,这是您需要的两个例程。只需添加一个新的 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;
    }
}

我希望这对您将来有所帮助

于 2014-03-01T10:08:23.803 回答