Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,
Please help me to restricct the numeric value in datagrid column.

it shows following error.

Error 1 'Public Event KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.

***********source code**********************


VB
Private Sub datagridrequest_EditingControlShowing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles datagridrequest.EditingControlShowing
        If TypeOf e.Control Is TextBox Then

            Dim tb As TextBox = TryCast(e.Control, TextBox)
            tb.KeyPress -= New KeyPressEventHandler(AddressOf tb_KeyPress)
            If Me.datagridrequest.CurrentCell.ColumnIndex = 0 Then

                tb.KeyPress += New KeyPressEventHandler(AddressOf tb_KeyPress)
            End If
        End If
    End Sub
    Private Sub tb_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
        If Not (Char.IsDigit(e.KeyChar)) Then
            If e.KeyChar <> ControlChars.Back Then
                'allow the backspace key

                e.Handled = True
            End If
        End If

    End Sub
Posted
Updated 24-Oct-11 21:23pm
v2

Google is your friend :)
Custom Numeric Edit Elements for DataGridView[^]
http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/ea4f5fce-7363-4bab-962b-7469a3886311/[^]
http://bytes.com/topic/c-sharp/answers/495386-implementing-datagridview-numeric-only-column[^]
I also noticed you use C# syntax in VB... tb.KeyPress += New KeyPressEventHandler(AddressOf tb_KeyPress)
Perhaps this is perfectly valid and I just never seen it. But in VB you usually use AddHandler tb.KeyPress, AddressOf tb_KeyPress.
Most C# to VB converters have a hard time converting this.

Edit:
Actually this is the cause of your errormessage ;)
VB
AddHandler tb.KeyPress, AddressOf tb_KeyPress

is the only correct syntax in VB.
 
Share this answer
 
v2
I just combine from Jayasakthi & Solution from Rossel, and use in my code, it's work well in my vb.net code. thanks guys. bellow is the whole code.

VB
Private Sub dgvItemService_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles dgvItemService.EditingControlShowing
        If TypeOf e.Control Is TextBox Then
            Dim tb As TextBox = TryCast(e.Control, TextBox)
            If Me.dgvItemService.CurrentCell.ColumnIndex = 2 Then
                AddHandler tb.KeyPress, AddressOf tb_KeyPress
            End If
        End If
    End Sub


VB
Private Sub tb_KeyPress(sender As Object, 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


Just Change the dgvItemService with your own DataGridview Name.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900