dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online Memberssuraj
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Resources » Code Snippets » Winforms DataGrid

Validate a column in DataGridView


Posted Date:     Category: Winforms DataGrid    
Author: Member Level: Gold    Points: 10



 


Prevent the user from entering data other than numbers to a DataGridView cell

								
'EditingControlShowing event. This event is fired when the user tries to edit the content of a cell:
Private Sub DataGridView1_EditingControlShowing( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms. _
DataGridViewEditingControlShowingEventArgs) _
Handles DataGridView1.EditingControlShowing

'---restrict inputs on the Amount Field---
If Me.DataGridView1.CurrentCell.ColumnIndex = DataGridView1.Columns("Amount").Index And Not e.Control Is Nothing Then

Dim tb As TextBox = CType(e.Control, TextBox)

'---add an event handler to the TextBox control---
AddHandler tb.KeyPress, AddressOf TextBox_KeyPress
End If

End Sub


'KeyPress event handler to the TextBox control that you want to restrict.
'This KeyPress event handler will be invoked when the user types into the cell and it is defined as follows:
Private Sub TextBox_KeyPress( _
ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.KeyPressEventArgs)

'---if textbox is empty and user pressed a decimal char---
If CType(sender, TextBox).Text = String.Empty And _
e.KeyChar = Chr(46) Then
e.Handled = True
Return
End If
'---if textbox already has a decimal point---
If CType(sender, TextBox).Text.Contains(Chr(46)) And _
e.KeyChar = Chr(46) Then
e.Handled = True
Return
End If
'---if the key pressed is not a valid decimal number---
If (Not (Char.IsDigit(e.KeyChar) Or _
Char.IsControl(e.KeyChar) Or _
(e.KeyChar = Chr(46)))) Then
e.Handled = True
End If
End Sub





Did you like this resource? Share it with your friends and show your love!


Responses to "Validate a column in DataGridView"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Insert a blank row at a particular position in a datagrid
    Previous Resource: Copy the content of the GridView control to a clipboard
    Return to Resources
    Post New Resource
    Category: Winforms DataGrid


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    (No tags found.)



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.