Solved

Changing Values in a datagridview

Posted on 2011-03-11
2
467 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
2 Comments
 
LVL 17

Accepted Solution

by:
nepaluz earned 500 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

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

13 Experts available now in Live!

Get 1:1 Help Now