DataGridView DataError ?

Posted on 2009-12-16
Last Modified: 2013-11-08
Using the DataGridView, Bound, on a numeric column when backspacing getting an error:
 "Input String was not in a correct format". How can I prevent this error? Or at least rever from it?
Question by:Jess31
    LVL 51

    Expert Comment

    is BackSpace casuing a postback?
    and after postback are you trying to update db?

    can you please post related html and aspx code...

    Author Comment

    I'm using VB.NET and Winform. Not ASP.
    LVL 51

    Expert Comment

    do you have any event on keypress? maybe keydown or keyup? or change event?
    check those, maybe there is a code running and causing this issue...

    Author Comment

    No Event on Keydown or Keyup.
    I don't believe its' code causing this but some how cause it is bound to a  numberic field it does not like the backspace ...
    LVL 15

    Accepted Solution


    Did you by any chance change the
    DGVColumn.DefaultCellStyle.NullValue ? It should be an empty string (which
    is the default).

    check if the DefaultCellStyle.NullValue property of your
    DataGridView control has been set to an empty string. DataGridView will convert an empty string to DBNull value.

    If it still gives an error then you will have to do some workaround withe cell_Leave event like

    Private Sub DataGridView1_CellLeave(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellLeave
            If Me.DataGridView1.Rows(e.RowIndex).Cells(2).EditedFormattedValue <> "" And IsNumeric(Me.DataGridView1.Rows(e.RowIndex).Cells(2).EditedFormattedValue) Then
                Me.DataGridView1.Columns(5).DefaultCellStyle.Format = "N"
            ElseIf Me.DataGridView1.Rows(e.RowIndex).Cells(2).EditedFormattedValue <> "" And IsNumeric(Me.DataGridView1.Rows(e.RowIndex).Cells(2).EditedFormattedValue) Then
                Me.DataGridView1.Rows(e.RowIndex).Cells(2).Value = DBNull.Value
            End If
        End Sub

    Author Closing Comment

    You hit it right on!
    That was the problem. DefaultCellStyle.NullValue.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now