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

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

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.
0
sandya_116
Asked:
sandya_116
1 Solution
 
wsh2Commented:
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
---------------------------------------------------------------------------------
0
 
sandya_116Author Commented:
My grid is not bound.
0
 
wsh2Commented:
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
sandya_116Author Commented:
I have multiple columns and rows in the grid so it might get complicated. Is there an easier way to do it?
0
 
wsh2Commented:
A common way of doing this, is upon ANY DataGridView change event.. set a module level boolean flag to True stating the Grid Is Dirty (ie. modified). Eg.
    m_booMyGridViewIsDirty = True
Test this flag to know if you need to save or not. (Note.. make sure to set this flag to False when you load the Grid and everything is still pristine)

This method definitely works, however, you may get false readings in the case when a user changes the original value from "A" to "B".. and then changes the cell back to (the original value) of "A". The IsDirty flag will note the change, even though the data is really what it originally was. You'll have to be the judge of how frequently this happens, which in most cases is very rare, and acceptable for most applications.

One last plug for the previous Tag method. Should you need to reset the values in the grid to the originals.. all you have to do is copy the original values in the Tags back into the Values. This can be done in a simple loop, rather than having to go through the whole rebuild process.

0
 
jpaulinoCommented:
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.
0
 
SanclerCommented:
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
0
 
sandya_116Author Commented:
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.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

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