Results 1 to 11 of 11

Thread: Restrict Datagridview column to only whole number

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2016
    Posts
    1,415

    Restrict Datagridview column to only whole number

    Hi,

    I have a dgv that is use tableadapter and bindingsource. The column is for "StepNo" meaning:

    Steps:

    1
    2
    3
    4
    5

    etc

    But some people can be so annoying... go put

    a
    b
    c
    d


    or

    1.1
    1.2
    1.3
    1.4

    How can I prevent user from typing in there only smallint values?

    Thanks

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

    Re: Restrict Datagridview column to only whole number

    Is your grid bound to a DataTable? If so then the data type of the bound DataColumn should prevent invalid data being added. If it's not bound then you can handle the CellValidating event and validate pretty much exactly as you would a regular TextBox. In both cases, it won't stop the user typing invalid values but they won't be able to leave that cell without fixing it.

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

    Re: Restrict Datagridview column to only whole number

    I see that you said that you are using a BindingSource so I assume that it must be bound. What is the data type of the DataColumn bound to that grid column?

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2016
    Posts
    1,415

    Re: Restrict Datagridview column to only whole number

    Hello John,

    I hope you are well.

    Yes it is bound. The data type is smallint (in the database)

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2016
    Posts
    1,415

    Re: Restrict Datagridview column to only whole number

    It does indeed as you are saying gives an error.. I very long description (when the user types the wrong format in the cell) perhaps i should leave it like that but maybe just make my own error message to tell the user to use whole numbers.. what event shall i then use? meaning... when is this error fired? I not sure if am asking correctly now...

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2016
    Posts
    1,415

    Re: Restrict Datagridview column to only whole number

    this is the error, then how to make my own error message description? Can you please tell me?

    Attachment 160341

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

    Re: Restrict Datagridview column to only whole number

    Check out the last line in that error dialogue. It tells you what to do.

    That said, that DataError event is raised when an attempt is made to push invalid data down to the underlying data source. I think that that may occur after the CellValidating event has completed anyway so, even with bound data, you should still be able to handle CellValidating and catch bad data before that exception is thrown.

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2016
    Posts
    1,415

    Re: Restrict Datagridview column to only whole number

    Thank you very much John!!

  9. #9
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: Restrict Datagridview column to only whole number

    Hi,

    looks like you got it solved.

    here a sample anyway to prevent the User entering something in a Column that
    they should'nt

    Code:
    Public Class Form3
    
        Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            With DataGridView1
                .Columns.Add("Numbers only", "Numbers only")
                .Columns.Add("whatever", "whatever")
                .Rows.Add(5)
            End With
        End Sub
    
        Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, _
           ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) _
           Handles DataGridView1.EditingControlShowing
    
            If TypeOf (e.Control) Is DataGridViewTextBoxEditingControl Then
                Dim locTB As DataGridViewTextBoxEditingControl = _
                                    CType(e.Control, DataGridViewTextBoxEditingControl)
                RemoveHandler locTB.KeyPress, AddressOf TextBox_KeyPress
                If DataGridView1.CurrentCell.ColumnIndex = 0 Then '<-- Column for Numbers
                    AddHandler locTB.KeyPress, AddressOf TextBox_KeyPress
                End If
            End If
        End Sub
    
        Private Sub TextBox_KeyPress(ByVal sender As Object, ByVal e As  _
                KeyPressEventArgs)
            If Not IsNumber(e.KeyChar) Then
                e.Handled = True
            End If
        End Sub
    
        Public Function IsNumber(ByVal myNum As Char) As Boolean
            Return ("1234567890" & Keys.Back & Keys.Delete).Contains(myNum)
        End Function
    End Class
    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

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

    Re: Restrict Datagridview column to only whole number

    Quote Originally Posted by ChrisE View Post
    Hi,

    looks like you got it solved.

    here a sample anyway to prevent the User entering something in a Column that
    they should'nt

    Code:
    Public Class Form3
    
        Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            With DataGridView1
                .Columns.Add("Numbers only", "Numbers only")
                .Columns.Add("whatever", "whatever")
                .Rows.Add(5)
            End With
        End Sub
    
        Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, _
           ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) _
           Handles DataGridView1.EditingControlShowing
    
            If TypeOf (e.Control) Is DataGridViewTextBoxEditingControl Then
                Dim locTB As DataGridViewTextBoxEditingControl = _
                                    CType(e.Control, DataGridViewTextBoxEditingControl)
                RemoveHandler locTB.KeyPress, AddressOf TextBox_KeyPress
                If DataGridView1.CurrentCell.ColumnIndex = 0 Then '<-- Column for Numbers
                    AddHandler locTB.KeyPress, AddressOf TextBox_KeyPress
                End If
            End If
        End Sub
    
        Private Sub TextBox_KeyPress(ByVal sender As Object, ByVal e As  _
                KeyPressEventArgs)
            If Not IsNumber(e.KeyChar) Then
                e.Handled = True
            End If
        End Sub
    
        Public Function IsNumber(ByVal myNum As Char) As Boolean
            Return ("1234567890" & Keys.Back & Keys.Delete).Contains(myNum)
        End Function
    End Class
    regards
    Chris
    The thing to consider there is that, just like for a regular TextBox, that won't prevent users pasting invalid text into a cell. Unless you're going to implement something more complex than that, you still need to handle CellValidating or DataError for those cases. It's also not easy to extend to other than integers, so will be inconsistent if you want to validate other columns too.

    Also, rather than that IsNumber method, I'd tend to go with:
    vb.net Code:
    1. If Not Char.IsDigit(e.KeyChar) AndAlso Not Char.IsControl(e.KeyChar) Then

  11. #11
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: Restrict Datagridview column to only whole number

    Quote Originally Posted by jmcilhinney View Post
    The thing to consider there is that, just like for a regular TextBox, that won't prevent users pasting invalid text into a cell. Unless you're going to implement something more complex than that, you still need to handle CellValidating or DataError for those cases. It's also not easy to extend to other than integers, so will be inconsistent if you want to validate other columns too.

    Also, rather than that IsNumber method, I'd tend to go with:
    vb.net Code:
    1. If Not Char.IsDigit(e.KeyChar) AndAlso Not Char.IsControl(e.KeyChar) Then
    good point JMC,
    thanks

    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

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