Link to home
Start Free TrialLog in
Avatar of sandya_116
sandya_116

asked on

Datagridview Check If cell values have changed

I have two datagridviews datagridview1 and datagridview2. When user selects a row in datagridview1, the appropriate information is loaded onto datagridview2. I also have a Save button which user clicks when they make changes in datagridview2. If user makes changes in datagridview2 and doesn't click on Save before selecting another row in a datagridview1, I want to show a warning message telling user to save the changes made in datagridview2. How can I do that? datagridview2 is not bound to a datatable. Thanks.
Avatar of wsh2
wsh2

If your grids are bound, you can test the underlying DataTable:
---------------------------------------------------------------------------------
   Dim booIsDirty As Boolean = False
   For Each objRow As System.Data.DataRow In myDataTable.Rows
      If objRow.RowState = DataRowState.Modified Then
         booIsDirty = True
         Exit For
      End If
   Next objRow
---------------------------------------------------------------------------------
Avatar of sandya_116

ASKER

My grid is not bound.
Suggestion..

When you load your grid.. place the original value in the Tag property of the cell. When the user clicks.. iterate the rows / cells and test to see if .Value = .Tag. If they are different, throw up your do you want to save message.
I have multiple columns and rows in the grid so it might get complicated. Is there an easier way to do it?
ASKER CERTIFIED SOLUTION
Avatar of wsh2
wsh2

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jorge Paulino
You can add two variables one string and one boolean in the beginning. Then when you select a cell you store is value in your string variable and after cellendedit you compare the string with the cell value. If is different you set the boolean variable to true and use it whenever you need.

On lost focus set the boolean to false.
This may be off-topic (or at least perilously close to the fringes of the topic) but I have to ask: why is the datagridview2 not bound?  The fact that changes to it may need to be saved suggests that it might be appropriate for binding.  Although it is, as you obviously know, possible to use a datagridview in unbound mode, I am never personally sure what the advantages are.  And with data that may need to be saved I begin to suspect that any advantages that there may be start to be outweighed by the disadvantages.

My view, here, is that it is just as easy to copy a record from one datatable into another datatable as it is to copy a record from a bound datagridview into an unbound one.  And if you do that, you probably make saving much easier.  And - and this is where we actually come back to the topic - you have ready made methods in datatables for detecting if a record is dirty.

Roger
I agree that bounding datagridview2 to a datatable would solve my problems but I just didn't have the time to do all that. To get this done fast, I used wsh2 method of checking on datagridview change event and it is working fine. If I get time, I will use a datatable. Thanks everyone for answering.