user2431035
user2431035

Reputation: 45

Only Allow to enter Numeric values Datagridview specific column

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

Upvotes: 4

Views: 31628

Answers (4)

matzone
matzone

Reputation: 5719

Use datagridview Editingcontrolshowing .. Basicly like this

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 ...

Upvotes: 3

user3614548
user3614548

Reputation: 1

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

Upvotes: 0

RekhaShanmugam
RekhaShanmugam

Reputation: 101

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

Upvotes: 9

Pieter
Pieter

Reputation: 91

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.

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:

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

Upvotes: 4

Related Questions