仅允许输入数字值Datagridview特定列

时间:2013-06-10 06:18:02

标签: c# datagridview

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

4 个答案:

答案 0 :(得分:9)

    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;
        }
    }

答案 1 :(得分:4)

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

更好的解决方案是:

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;
    }
}

Event 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);
}

答案 2 :(得分:2)

使用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 EventHandler(CheckKey);

     }
}

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

您可以改进此CheckKey ...

答案 3 :(得分:0)

   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;
            }
        }