Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: DataGrid
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**********************
 

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 24-Oct-11 22:22pm
Edited 24-Oct-11 22:23pm
sravani.v15.7K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Google is your friend Smile | :)
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 Wink | ;)
AddHandler tb.KeyPress, AddressOf tb_KeyPress
is the only correct syntax in VB.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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

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



Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 25 Mar 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100