1

I am working on with DatagridView tool. Here I am doing a manual entry on columns and save it directly to database. I have 5 columns out of which 3 are alphanumeric and 2 are numeric columns.

I have set condition to for numeric columns using handlers in EditingControlShowing event.

    If grdLedgerDetails.CurrentCell.ColumnIndex = 4 Then
        AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBox_keyPress
    ElseIf grdLedgerDetails.CurrentCell.ColumnIndex = 5 Then
        AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBox_keyPress
    End If


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

This condition works properly. But the problem is that, this condition works on all columns. I just want it to be worked on specified column.

Please help me out from this.

Thanks in advance.

2 Answers 2

1

This works fine.

 Private Sub grdLedgerDetails_EditingControlShowing(sender As Object, e As                    System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles grdLedgerDetails.EditingControlShowing
    Select grdLedgerDetails.CurrentCell.ColumnIndex
        Case 2, 3
            RemoveHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBox_keyPress
        Case 4, 5
            AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBox_keyPress
    End Select
End Sub



Private Sub TextBox_keyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
    If Not Char.IsControl(e.KeyChar) And Not Char.IsDigit(e.KeyChar) And e.KeyChar <> "." Then
        e.Handled = True
    End If
       End Sub
Sign up to request clarification or add additional context in comments.

Comments

0

Try like this

Private Sub dgv_EditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs)
    RemoveHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBox_keyPress
    AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBox_keyPress
End Sub

Private Sub TextBox_keyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
    Select Case grdLedgerDetails.CurrentCell.ColumnIndex
        Case 4,5
            If Char.IsDigit(CChar(CStr(e.KeyChar))) = False Then e.Handled = True
    End Select
End Sub

6 Comments

sorry the problem is.. When i first enter in alphanumeric columns, it works fine. But once i do changes in numeric columns and try to edit in alphanumeric columns, it takes the format of numeric columns and not allowing to type the characters. Any idea regarding this issue?
@charu have you tried e.ColumnIndex instead of CurrentCell.ColumnIndex?
Sorry @charu for late. But, why are you handling keydown event for numeric only cell. I mean what happen when user copy some alpha numeric text and pate in that cell. In that case application will allow user to enter non-numberic values. So, My suggestion is use javascript events or RegularExpressionValidator to handle numeric values. I have updated my answer.
@shell.Thank you for the reply. It will be fine on web forms. But i am working on windows forms.
oops sorry. Thank you.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.