Hi sandsdad,
First you need to create an extended DataGridView have a property called "IsCellValueChanged". The code of the DataGridView is like this.
public class ExtDataGridView : DataGridView
{
private bool isCellValueChanged;
public bool IsCellValueChanged
{
get { return isCellValueChanged; }
set { isCellValueChanged = value; }
}
public ExtDataGridView()
{
isCellValueChanged = false;
}
protected override void OnCellValueChanged(DataGridViewCellEventArgs e)
{
isCellValueChanged = true;
base.OnCellValueChanged(e);
}
}
When the cellvalue is changed, the IsCellValueChanged will be set to true.
Second, you can handle the NodeMouseClick event of TreeView to check if any cell is changed in DataGridView.
void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Node != treeView1.SelectedNode)
{
if (dataGridView1.IsCellValueChanged == true)
{
MessageBox.Show("Do you want to save the changed data?");
// Take action here.
}
}
}
Third, handle the FormClosing event of the form to check the DataGridView cell value changed.
void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (dataGridView1.IsCellValueChanged == true)
{
if (MessageBox.Show("Do you want to save changed data?", "Notice", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
{
e.Cancel = true;
}
}
}
Once user click the save button to save changed data, remember to set the IsCellValueChanged to false.
Hope this helps.
Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.