48

我需要制作仅在按键事件中仅接受特定列的数值的 datagridview。有没有最好的方法来做到这一点?

4

7 回答 7

76
  • 添加EditingControlShowing事件
  • 在 EditingControlShowing 中,检查当前单元格是否位于所需的列中。
  • 在 EditingControlShowing 中注册一个新的 KeyPress 事件(如果上述条件为真)。
  • 删除之前在 EditingControlShowing 中添加的任何 KeyPress 事件。
  • 在 KeyPress 事件中,检查如果键不是数字则取消输入。

例子:

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    e.Control.KeyPress -= new KeyPressEventHandler(Column1_KeyPress);
    if (dataGridView1.CurrentCell.ColumnIndex == 0) //Desired Column
    {
        TextBox tb = e.Control as TextBox;
        if (tb != null)
        {
            tb.KeyPress += new KeyPressEventHandler(Column1_KeyPress);
        }
    }
}

private void Column1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
    {
        e.Handled = true;
    }
}
于 2013-02-01T03:35:05.097 回答
35

您必须像这样使用DataGridView.CellValidating 事件

    private void dataGridView1_CellValidating(object sender, 
                                           DataGridViewCellValidatingEventArgs e)
    {
        if (e.ColumnIndex == 1) // 1 should be your column index
        {
            int i;

            if (!int.TryParse(Convert.ToString(e.FormattedValue), out i))
            {
                e.Cancel = true;
                label1.Text ="please enter numeric";
            }
            else
            {
                // the input is numeric 
            }
        }
    }
于 2012-09-28T19:34:45.753 回答
9
 private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        e.Control.KeyPress -= new KeyPressEventHandler(Column1_KeyPress);
        if (dataGridView1.CurrentCell.ColumnIndex == 4) //Desired Column
        {
            TextBox tb = e.Control as TextBox;
            if (tb != null)
            {
                tb.KeyPress += new KeyPressEventHandler(Column1_KeyPress);
            }
        }

    }
    private void Column1_KeyPress(object sender, KeyPressEventArgs e)
    { 
          // allowed only numeric value  ex.10
        //if (!char.IsControl(e.KeyChar)
        //    && !char.IsDigit(e.KeyChar))
        //{
        //    e.Handled = true;
        //}

               // allowed numeric and one dot  ex. 10.23
        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;
        }
    }
于 2013-09-17T05:25:09.837 回答
4

除非您像其他人指出的那样需要小数位,否则给出的答案非常好。在这种情况下,您需要扩展验证,在下面添加 using 和 vars 以获得小数分隔符的区域性变量值

using System.Globalization;

NumberFormatInfo nfi = Thread.CurrentThread.CurrentCulture.NumberFormat;
char decSeperator;

decSeperator = nfi.CurrencyDecimalSeparator[0];

将验证扩展到:

if (!char.IsControl(e.KeyChar) && !(char.IsDigit(e.KeyChar) 
| e.KeyChar == decSeperator))
{
    e.Handled = true;
}
// only allow one decimal point
if (e.KeyChar == decSeperator
    && (sender as TextBox).Text.IndexOf(decSeperator) > -1)
{
    e.Handled = true;
}
于 2015-10-12T11:48:40.263 回答
2
Private WithEvents txtNumeric As New DataGridViewTextBoxEditingControl

Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing

txtNumeric = CType(e.Control, DataGridViewTextBoxEditingControl)
End Sub

Private Sub txtNumeric_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtNumeric.KeyPress
    If (DataGridView1.CurrentCell.ColumnIndex > 0) Then
        If (Not Char.IsControl(e.KeyChar) And Not Char.IsDigit(e.KeyChar) And Not e.KeyChar = ".") Then
            e.Handled = True
        Else
            'only allow one decimal point
            If (e.KeyChar = "." And txtNumeric.Text.Contains(".")) Then
                e.Handled = True
            End If
        End If
    End If
End Sub
于 2015-08-17T11:27:00.267 回答
0

您也可以尝试这种方式,接受小数字符

    private void Column1_KeyPress(object sender, KeyPressEventArgs e) 
    {
        //allow number, backspace and dot
        if (!(char.IsDigit(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == '.'))
        {
            e.Handled = true;

        }
        //allow only one dot
        if (e.KeyChar == '.' && (sender as TextBox).Text.Contains("."))
        {
            e.Handled = true;

        }
    }
于 2019-05-18T05:33:02.090 回答
0

我正在做一个矩阵计算器,并且正在使用两个 DataGridView 对象。这是一个对我有用的代码。我从这篇文章中获取了第一条评论并对其进行了一些修改。

//Adding characters to a cell
    private void dataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        
            if (e.Control != null)
            {
                e.Control.KeyPress += new KeyPressEventHandler(Column1_KeyPress);
                Console.WriteLine(e.Control.Text);
            }
    }
    //Handling presses for minus dot and numbers
    private void Column1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '-' && e.KeyChar != '.')
            e.Handled = true;
        if (e.KeyChar == '.')
        {
            if (((DataGridViewTextBoxEditingControl)sender).Text.Length == 0)
                e.Handled = true;
            if (((DataGridViewTextBoxEditingControl)sender).Text.Contains('.'))
                e.Handled = true;
        }
        if (e.KeyChar == '-')
        {
            if (((DataGridViewTextBoxEditingControl)sender).Text.Length != 0)
                e.Handled = true;
            if (((DataGridViewTextBoxEditingControl)sender).Text.Contains('-'))
                e.Handled = true;
        }
    }
于 2021-12-19T22:10:54.577 回答