DataGridView Numeric Only Cell?

SamuraiJack

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 
Sathish

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

DataGridView Numeric Only Cell?

From Dev

How to colorize current cell only in DataGridView

From Dev

Multiline datagridview cell to display First Line only

From Dev

Check if Excel cell text is numeric using formula only

From Dev

Change color of only part of text inside a DataGridView cell

From Dev

KeyUp event fire for only specific cell in datagridview c#

From Dev

DataGridView: Disable Cell Editing on Click or Keystroke, Enable Cell Editing only on Double Click

From Dev

Change of the edited cell in a datagridview cell

From Dev

Handsontable numeric cell globalization

From Dev

Cell numeric value

From Dev

Cell Formatting In DataGridView On DataBindingComplete

From Dev

Casting datagridview cell as float

From Dev

Multiline text in DataGridView Cell

From Dev

Custom DataGridView Cell Painting

From Dev

Datagridview cell with background image

From Dev

DataGridView Cell Value Changing

From Dev

Apply Formula Cell to a DataGridview

From Dev

How to delete a datagridview cell?

From Dev

comparison of datagridview cell values

From Dev

Datagridview coloring cell

From Dev

DataGridView Cell Value Changing

From Dev

Colour a cell in datagridview

From Dev

Updating A DataGridView Cell Incrementally

From Dev

Datagridview Header cell

From Dev

DataGridView double underline Cell

From Dev

Hiding a value in a datagridview cell

From Dev

Iterate Each Cell Of DataGridView

From Dev

Export from datagridview to *.txt file only first 20 cell's letters

From Dev

trying to fill a Datagridview in C# with values from a JSON but i only get 1 cell fill