Results 1 to 7 of 7

Thread: [RESOLVED] control datagridview input characters

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830

    [RESOLVED] control datagridview input characters

    I have a datagridview (dgvSalesOrderLines ) that has 5 columns. The types are:
    decimal | character | character | decimal | decimal

    I am trying to get it so if the user adds a new row that for columns 0, 3 and 4 that only numeric / decimal data is allowed. The following works the first column but after that nothing works.

    Looking for any help on this.
    Thanks.


    Code:
    Private Sub dgvSalesOrderLines_EditingControlShowing(sender As Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgvSalesOrderLines.EditingControlShowing
            Dim iCurCol As Integer = dgvSalesOrderLines.CurrentCell.ColumnIndex
            Select Case iCurCol
                Case 0, 3, 4
                    'only allow numerics
                    If TypeOf e.Control Is TextBox Then
                        Dim tb As TextBox = TryCast(e.Control, TextBox)
                        AddHandler tb.KeyPress, AddressOf dgv_KeyPress
                    End If
                Case Else
                    
            End Select
        End Sub
    
        Private Sub dgv_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
            If Not (Char.IsNumber(e.KeyChar)) Then
                e.Handled = True
            End If
        End Sub
    Last edited by lleemon; Oct 5th, 2012 at 06:47 AM. Reason: resolved

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: control datagridview input characters

    [QUOTE]I am trying to get it so if the user adds a new row that for columns 0, 3 and 4 that only numeric / decimal data is allowed./QUOTE]

    Parameters! They're amazingly simple to use and even better, they're easy to read. An example of adding a dgv row to an unbound datagridview would be:

    Code:
    Private Sub addRow(ByVal decimal1 As Decimal, ByVal numerical1 As Integer, ByVal numerical2 As Decimal _
                           , ByVal decimal3 As Decimal, ByVal decimal4 As Decimal)
    
            'Declare a datagridviewrow
            Dim dataRow As New DataGridViewRow
    
            'Set the cell's values
            With dataRow
                .Cells(0).Value = decimal1
                .Cells(1).Value = numerical1
                .Cells(2).Value = numerical2
                .Cells(3).Value = decimal2
                .Cells(4).Value = decimal3
            End With
    
            'Insert the row
            DataGridView1.Rows.Add(dataRow)
    
        End Sub
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: control datagridview input characters

    One issue is the fact that you're not removing the event handler. If you use AddHandler then you must ALWAYS have a corresponding RemoveHandler. The logical place to put it would be the CellEndEdit event handler.

    Also, you say that the data type is decimal yet you're only allowing numeric characters. What about a decimal point? The user won't be able to delete or backspace with that code either.

    Finally, what if the user pastes non-numeric data into a cell?

    These are examples of why limiting the user to numeric input in a TextBox is not nearly as simple as it first seems. I've posted a fairly rigorous implementation of a numeric text box in the CodeBank and you could create a custom cell and column class for that. You then wouldn't need any extra code.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: control datagridview input characters

    I will point out another non-fatal issue in your code too:
    Code:
                        Dim tb As TextBox = TryCast(e.Control, TextBox)
                        AddHandler tb.KeyPress, AddressOf dgv_KeyPress
    The whole point of TryCast is that it will try to cast the specified reference as the specified type and return Nothing if it fails. The fact that you're using TryCast implies that you acknowledge that it might fail and return Nothing, but then you go ahead and reference the KeyPress event of the TextBox anyway. That's going to throw a NullReferenceException if 'tb' is Nothing so, if 'tb' can be Nothing, you should be testing for that first. If 'tb' can't be Nothing then why are you using TryCast in the first place instead of DirectCast?

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830

    Re: control datagridview input characters

    In _CellEndEdit, how do I get control of the textbox to remove the handle?

    Code:
    Private Sub dgvSalesOrderLines_EditingControlShowing(sender As Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgvSalesOrderLines.EditingControlShowing
            Dim iCurCol As Integer = dgvSalesOrderLines.CurrentCell.ColumnIndex
            Select Case iCurCol
                Case 0, 3, 4
                    'only allow numerics
                    If TypeOf e.Control Is TextBox Then
                        Dim tb As TextBox = DirectCast(e.Control, TextBox)
                        If tb IsNot Nothing Then
                            AddHandler tb.KeyPress, AddressOf dgv_KeyPress
                        End If
                    End If
                Case Else
    
            End Select
        End Sub
    
        Private Sub dgv_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
            If Not (Char.IsNumber(e.KeyChar)) Then
                If e.KeyChar <> "." Then
                    e.Handled = True
                End If
            End If
        End Sub
    
        Private Sub dgvSalesOrderLines_CellEndEdit(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvSalesOrderLines.CellEndEdit
            Dim tb As TextBox = DirectCast(e.Control, TextBox) '<-- e.Control error is 'Control' is not a member of 'System.Windows.Form.DataGridViewCellEventArgs'
            RemoveHandler tb.KeyPress, AddressOf dgv_KeyPress
        End Sub

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: control datagridview input characters

    Quote Originally Posted by lleemon View Post
    In _CellEndEdit, how do I get control of the textbox to remove the handle?
    The obvious option would be the EditingControl property of the grid. I'm not sure whether that will be Nothing at that point or not. If it's not then you're good to go. If it is then you'd have to use your own member variable to assign the control to in the first place so you can get access to it. The thing is, if you're going to declare a field anyway then you may as well do what I usually do, which means that you don;t have to use AddHandler or RemoveHandler, e.g.
    vb.net Code:
    1. Private WithEvents editingTextBox As TextBox
    2.  
    3. Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
    4.     editingTextBox = TryCast(e.Control, TextBox)
    5. End Sub
    6.  
    7. Private Sub DataGridView1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
    8.     editingTextBox = Nothing
    9. End Sub
    10.  
    11. Private Sub editingTextBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles editingTextBox.KeyPress
    12.     '...
    13. End Sub
    Because the field is declared WithEvents you get to use a Handles clause. Whatever's assigned to that field will have its event handled by that method, so simply setting the field to Nothing detaches the event handler.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830

    Re: control datagridview input characters

    jmcilhinney - your last post works beautiful. Appreciate your help. Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width