?
Solved

DataGridView DataError ?

Posted on 2009-12-16
6
Medium Priority
?
760 Views
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?
0
Comment
Question by:Jess31
  • 3
  • 2
6 Comments
 
LVL 61

Expert Comment

by:HainKurt
ID: 26067017
is BackSpace casuing a postback?
and after postback are you trying to update db?

can you please post related html and aspx code...
0
 
LVL 1

Author Comment

by:Jess31
ID: 26067057
I'm using VB.NET and Winform. Not ASP.
0
 
LVL 61

Expert Comment

by:HainKurt
ID: 26067441
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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 1

Author Comment

by:Jess31
ID: 26068789
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
 
LVL 15

Accepted Solution

by:
rajeeshmca earned 2000 total points
ID: 26069377
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
 
LVL 1

Author Closing Comment

by:Jess31
ID: 31667031
You hit it right on!
That was the problem. DefaultCellStyle.NullValue.
Thanks!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Loops Section Overview
Suggested Courses

840 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