?
Solved

Changing Values in a datagridview

Posted on 2011-03-11
2
Medium Priority
?
502 Views
Last Modified: 2012-05-11
I had asked how to check to see if a cell in a datagridview was changed so I could prompt the user if they wanted to save the changes.  The solution was to set the value of a global variable "CellBegin" (which I store as a string) with the cell contents on the begin edit and then compare it on the cell end edit.  It works very nicely for existing rows in the data.  However, as soon as I begin to enter a new record I get the following error message:

Conversion from type 'DBNull' to type 'String' is not valid.

Should I change the variable to something other than a string (variant?).  Or is there a way to recognize this entry is a new record and then bypass the compare?  Hope I'm making sense.
Private Sub DataGridView1_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles DataGridView1.CellBeginEdit
        CellBegin = Me.DataGridView1.CurrentCell.Value
    End Sub

Open in new window

0
Comment
Question by:bhlabelle
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 17

Accepted Solution

by:
nepaluz earned 2000 total points
ID: 35110783
just encapsulate the statement in a try catch and set the CellBegin value to nothing, e.g

Private Sub DataGridView1_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles DataGridView1.CellBeginEdit
   Try
        CellBegin = Me.DataGridView1.CurrentCell.Value
   Catch ex As Exception
        CellBegin = Nothing
   End Try
End Sub
0
 

Author Closing Comment

by:bhlabelle
ID: 35110895
I don't know why, but I always forget to do things in a try catch.  Thank goodness I'm not a real programmer.

Thanks for the help!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

752 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