DataGridView Numeric Only Cell?

42,813

Solution 1

Try this code

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

        If DataGridView1.CurrentCell.ColumnIndex = 2 Then

            AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBox_keyPress

        ElseIf DataGridView1.CurrentCell.ColumnIndex = 1 Then

            AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBox_keyPress1


        End If

    End Sub

    Private Sub TextBox_keyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)

        If Char.IsDigit(CChar(CStr(e.KeyChar))) = False Then e.Handled = True

    End Sub

    Private Sub TextBox_keyPress1(ByVal sender As Object, ByVal e As KeyPressEventArgs)

        If Not (Char.IsDigit(CChar(CStr(e.KeyChar))) Or e.KeyChar = ".") Then e.Handled = True

    End Sub

TextBox_keyPress Event for only numeric

TextBox_keyPress1 Event for numeric with decimal value

Solution 2

If data type validation is only concern then you can use CellValidating event like this

    private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
    {
        //e.FormattedValue  will return current cell value and 
        //e.ColumnIndex & e.RowIndex will rerurn current cell position

        // If you want to validate particular cell data must be numeric then check e.FormattedValue is all numeric 
        // if not then just set  e.Cancel = true and show some message 
        //Like this 

        if (e.ColumnIndex == 1)
        {
            if (!IsNumeric(e.FormattedValue))  // IsNumeric will be your method where you will check for numebrs 
            {
                MessageBox.Show("Enter valid numeric data");
                dataGridView1.CurrentCell.Value = null;
                e.Cancel = true;

            }

        }

    }
Share:
42,813
SamuraiJack
Author by

SamuraiJack

Updated on July 23, 2022

Comments

  • SamuraiJack
    SamuraiJack almost 2 years

    I am new to winforms..I am trying to set two column of DataGridView to Numeric Only.. I do not want user to be able to type anything into a cell unless its a natural number in one column and a numeric value in another(which always one decimal). I thought this would be simple.. but even after trying a lot of things from stackoverflow and other sites i am still unable to achieve this.

    If DataGridView1.CurrentCell.ColumnIndex = 8 Then
    
        If Not Char.IsControl(e.KeyChar) AndAlso Not Char.IsDigit(e.KeyChar) AndAlso e.KeyChar <> "."c Then
            e.Handled = True
        End If
    
    End If 
    
  • SamuraiJack
    SamuraiJack over 10 years
    How do i clear the cell? I do not want user to be able to type if its not numeric
  • Neeraj Kumar Gupta
    Neeraj Kumar Gupta over 10 years
    Modified my post please check @Arbaaz
  • Neeraj Kumar Gupta
    Neeraj Kumar Gupta over 10 years
    added [dataGridView1.CurrentCell.Value = null;]
  • King King
    King King over 10 years
    @Arbaaz at least the other answer (of SATSON) should work, why did you not leave any comment? You should leave some comment on all the answers to notify them to improve their answer, otherwise you won't have any better answer.
  • SamuraiJack
    SamuraiJack over 10 years
    I am trying to validate DataGridViewTextBoxCell I am not using TextBox.
  • SamuraiJack
    SamuraiJack over 10 years
    @KingKing you are right .. my apologies .. I have left a comment there now.
  • SamuraiJack
    SamuraiJack over 10 years
    @NeerajKumarGupta yes its not clearing the cell.. the value remains as it is in the cell.
  • Sathish
    Sathish over 10 years
    yes. you are not using texbox. but you can achieve your goal by writing textbox event and add handler in the DataGridView1_EditingControlShowing event.. you no need to add text box in the form design..
  • SamuraiJack
    SamuraiJack over 10 years
    This my friend is by far the best solution I have come across so far.. I am sorry i took so long to respond. Thanks. However there is only one thing lacking, which is.. In case of decimal numbers only single decimal is to be allowed.
  • Sathish
    Sathish over 10 years
    @Arbaaz format the decimal cell after the editing or check the particular cell have decimal value. I think format (#,0.0) is the best sloution
  • SamuraiJack
    SamuraiJack over 10 years
    well i tried to check if the cell already has decimal but i always get null reference error ... I have asked this in a new question .. stackoverflow.com/q/19899456/2064292
  • d4Rk
    d4Rk almost 9 years
    Please also provide some explanation and dont just paste a chunk of code.
  • Ramgy Borja
    Ramgy Borja almost 7 years
    can you write it down in VB.NET @NeerajKumarGupta
  • Ramgy Borja
    Ramgy Borja almost 7 years
    used EditingControlShowing instead of CellValidating
  • Sudhakar MuthuKrishnan
    Sudhakar MuthuKrishnan about 6 years
    THIS CODE ALLOWS BACKSPACE ALSO & WORKS PRETTY!