Help with formating Cell in DataGridView

Hi

How do you force users to enter only 15 characters in a particular  column of a DatagridView  using VB.NET?

Thanks,

V.
vcharlesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

Maybe like this

CType(Me.DataGridView1.Columns(0), DataGridViewTextBoxColumn).MaxInputLength = 10  

Open in new window

Regards
1
vcharlesAuthor Commented:
Thanks, how do you force users to enter data in the following  format. 99,999,99? Where 9 are numeric values only.

V.
0
Éric MoreauSenior .Net ConsultantCommented:
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Rgonzo1971Commented:
Maybe
dataGridView1.Columns(0).DefaultCellStyle.Format = "00,000,00"
' and
Private Sub DataGridView1_KeyPress(sender As Object, e As     System.Windows.Forms.KeyPressEventArgs) Handles DataGridView1.KeyPress
    If (Char.IsDigit(e.KeyChar)) Then
        'this is a number
        e.Handled = True
    Else
        'not a number
    End If
End Sub

Open in new window

0
vcharlesAuthor Commented:
Thank you for the links.
0
Swamp_ThingCommented:
Here is a solution that lets you do two things:
    - Validate each character on KeyPress and suppress those that are invalid
    - Check the current cell value *PRIOR* to this character for content and/or format

My example below restricts the cell to numeric only with no more than one decimal point but you can change the validation to fit your needs.

e.Handled = True will BLOCK the character
e.Handled = False (the default state so it's implicite) will ALLOW the character

These Events fire BEFORE the character is added to the cell value.......

--- %< --- Cut Here --- %< --- Cut Here --- %< --- Cut Here --- %< --- Cut Here ---

    Private Sub dgv_EditingControlShowing(sender As Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgv.EditingControlShowing
        Dim txtEdit As TextBox = e.Control
        'remove any existing handler
        RemoveHandler txtEdit.KeyPress, AddressOf dgv_KeyPress
        AddHandler txtEdit.KeyPress, AddressOf dgv_KeyPress
    End Sub

    Private Sub dgv_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs)
        If Not String.Format("0123456789.{0}{1}", ChrW(Keys.Back), ChrW(Keys.Delete)).Contains(e.KeyChar) Then
            'Suppress non-numeric characters
            e.Handled = True
        Else
            If (e.KeyChar = ".") And (DirectCast(sender, TextBox).Text.IndexOf(".") > -1) Then
                'Only one decimal point per customer please
                e.Handled = True
            End If
        End If
    End Sub

--- %< --- Cut Here --- %< --- Cut Here --- %< --- Cut Here --- %< --- Cut Here ---
0
Swamp_ThingCommented:
In my sample above all you need in the dgv_KeyPress event handler is

--- %< --- Cut Here --- %< --- Cut Here --- %< --- Cut Here --- %< --- Cut Here ---

e.Handled = (DirectCast(sender, TextBox).Text.Length > 14

--- %< --- Cut Here --- %< --- Cut Here --- %< --- Cut Here --- %< --- Cut Here ---

If the Length is greater than 14 ( 15 or more ) then adding another character violates your rule...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vcharlesAuthor Commented:
Thank you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.