C# 仅允许输入数值 Datagridview 特定列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17017864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:21:13  来源:igfitidea点击:

Only Allow to enter Numeric values Datagridview specific column

c#datagridview

提问by user2431035

Is there any way to customize datagridview column to accept only numeric values. Also if user press any other character other than numbers nothing must type on the current cell.Is there any way to solve this problem

有什么方法可以自定义 datagridview 列以仅接受数值。此外,如果用户按下数字以外的任何其他字符,则无需在当前单元格上输入任何内容。有没有办法解决这个问题

采纳答案by matzone

Use datagridview Editingcontrolshowing .. Basicly like this

使用 datagridview Editingcontrolshowing .. 基本上是这样的

private void dataGridView1_EditingControlShowing(object sender, 
    DataGridViewEditingControlShowingEventArgs e)    
{
String sCellName =  dataGridView1.Columns(e.ColumnIndex).Name; 
    If (UCase(sCellName) == "QUANTITY") //----change with yours
    {

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

     }
}

private void CheckKey(object sender, KeyPressEventArgs e)
{
    if (!char.IsControl(e.KeyChar) 
        && !char.IsDigit(e.KeyChar) 
        && e.KeyChar != '.')
    {
        e.Handled = true;
    }   
}

You can improve this CheckKey ...

您可以改进此 CheckKey ...

回答by RekhaShanmugam

    private void gvAppSummary_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        if (gvAppSummary.CurrentCell.ColumnIndex == intRate)
        {
            e.Control.KeyPress += new KeyPressEventHandler(gvAppSummary_KeyPress);
        }
    }

    private void gvAppSummary_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
        {
            e.Handled = true;
        }
    }

回答by Pieter

With the previous solutions, every time you enter the EditingControlShowing event, you will add the KeyPressEvent in ?the list? of events to perform on KeyPress. This can easily be checked by setting a breakpoint in the KeyPress event.

使用之前的解决方案,每次进入EditingControlShowing 事件时,都会在列表中添加KeyPressEvent。要在 KeyPress 上执行的事件。这可以通过在 KeyPress 事件中设置断点来轻松检查。

Better solution would be:

更好的解决方案是:

private static KeyPressEventHandler NumericCheckHandler = new KeyPressEventHandler(NumericCheck);
private void dataGrid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    if (dataGrid.CurrentCell.ColumnIndex == numericColumn.Index)
    {
        e.Control.KeyPress -= NumericCheckHandler;
        e.Control.KeyPress += NumericCheckHandler;
    }
}

And the Event NumericCheck:

和事件 NumericCheck:

private static void NumericCheck(object sender, KeyPressEventArgs e)
{
    DataGridViewTextBoxEditingControl s = sender as DataGridViewTextBoxEditingControl;
    if (s != null && (e.KeyChar == '.' || e.KeyChar == ','))
    {
        e.KeyChar = System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator[0];
        e.Handled = s.Text.Contains(e.KeyChar);
    }
    else
        e.Handled = !char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar);
}

回答by user3614548

   e.Control.KeyPress -= new KeyPressEventHandler(Column18qty_KeyPress);
                if (dgvProduct.CurrentCell.ColumnIndex == 18) //dgvtxtQty
                {
                    TextBox tb = e.Control as TextBox;
                    if (tb != null)
                    {
                        tb.KeyPress += new KeyPressEventHandler(Column18qty_KeyPress);
                    }
                }
 private void Column18qty_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar)
            && e.KeyChar != '.')
            {
                e.Handled = true;
            }
            // only allow one decimal point
            if (e.KeyChar == '.'
                && (sender as TextBox).Text.IndexOf('.') > -1)
            {
                e.Handled = true;
            }
        }