-
Jul 16th, 2018, 05:52 AM
#1
Thread Starter
Frenzied Member
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
-
Jul 16th, 2018, 06:15 AM
#2
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.
-
Jul 16th, 2018, 06:17 AM
#3
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?
-
Jul 16th, 2018, 06:44 AM
#4
Thread Starter
Frenzied Member
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)
-
Jul 16th, 2018, 06:48 AM
#5
Thread Starter
Frenzied Member
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...
-
Jul 16th, 2018, 06:51 AM
#6
Thread Starter
Frenzied Member
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
-
Jul 16th, 2018, 06:56 AM
#7
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.
-
Jul 16th, 2018, 07:31 AM
#8
Thread Starter
Frenzied Member
Re: Restrict Datagridview column to only whole number
Thank you very much John!!
-
Jul 16th, 2018, 07:46 AM
#9
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.
-
Jul 16th, 2018, 08:22 AM
#10
Re: Restrict Datagridview column to only whole number
Originally Posted by ChrisE
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:
If Not Char.IsDigit(e.KeyChar) AndAlso Not Char.IsControl(e.KeyChar) Then
-
Jul 16th, 2018, 08:32 AM
#11
Re: Restrict Datagridview column to only whole number
Originally Posted by jmcilhinney
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:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|