DataGridView DataError ?

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?
LVL 1
Jess31Asked:
Who is Participating?
 
rajeeshmcaConnect With a Mentor Commented:
HI,

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
0
 
HainKurtSr. System AnalystCommented:
is BackSpace casuing a postback?
and after postback are you trying to update db?

can you please post related html and aspx code...
0
 
Jess31Author Commented:
I'm using VB.NET and Winform. Not ASP.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
HainKurtSr. System AnalystCommented:
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...
0
 
Jess31Author Commented:
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 ...
0
 
Jess31Author Commented:
You hit it right on!
That was the problem. DefaultCellStyle.NullValue.
Thanks!
0
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.

All Courses

From novice to tech pro — start learning today.