[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 513
  • Last Modified:

Changing Values in a datagridview

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
bhlabelle
Asked:
bhlabelle
1 Solution
 
nepaluzCommented:
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
 
bhlabelleAuthor Commented:
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now