Detecting value change in a datagrid cell

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Greetings,

I am trying to find out how to do something that on the surface seems like
it should be very simple to do.

I have a datagrid with a datatable bound to it using the SetDataBinding
method. What I am trying to accomplish is to detect if a value has changed
in any cell so that if a user closes the form without saving first, I want to
prompt to save changes. I don't want to prompt if there were no changes.

I tried doing it at the datatable level, but apparently changes to the
datagrid do not automatically get replicated to the underlying datasource
because that didn't work.

Is there some way to do this? I am using VS.NET 2003 and C#.

Thanks in advance for any help!

Mike
 
Hi Cor,

Thanks for taking the time. Actually, the fact that a change in the
datagrid doesn't automatically replicate to the datasource is a good thing in
my case since if they didn't want to save a change I wouldn't have to do
anything.

I did find a solution to what I was looking for by using the
CurrentCellChanged event. I found it from another post and edited it to work
for me:

private void dgPlayers_CurrentCellChanged(object sender, System.EventArgs e)
{
if(origValue==null)
{
origDataCell=this.dgPlayers.CurrentCell;
origValue=this.dgPlayers[this.dgPlayers.CurrentCell];
}
else
{
if(!origValue.Equals(this.dgPlayers[origDataCell]))
{
hasChanges = true;
}
origDataCell=this.dgPlayers.CurrentCell;
origValue=this.dgPlayers[this.dgPlayers.CurrentCell];
}

}

In the Closing event of the form I check the hasChanges variable for a true
state.

It works like a champ!

Thanks,

Mike
 
Back
Top