Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Question on how to handle pending updates
 

Question on how to handle pending updates

I have a form that has a treeview and datagridview. When the user clicks on an entry in the treeview, the filter is set in the datagridview and the data is displayed (and potentually modified). I need to have a way that if they modify a row, and dont save, then click on another entry (or closes the form for that matter), they will be warned that they have pending updates and do they want to update or discard. I'm not clear on how to handle this. I am using DataAdapters. Any suggestions?
sandsdad  Wednesday, June 10, 2009 9:50 PM

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.
Kira Qian  Friday, June 12, 2009 9:12 AM

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.
Kira Qian  Friday, June 12, 2009 9:12 AM

You can use google to search for other answers

Custom Search

More Threads

• [SOLVED] Selecting a certain cell?
• Pulling data from a DataSet using vb.net and then exporting to Excel?
• Update foreign key inside a Dataset
• MaskedTextBox bug!?
• datagrid view problem --
• Table Inheritance
• Datagridview - Cells ErrorText clearing
• programmatically select a cell in the datagrid
• Setting TextBox MaxLength from DataColumn MaxLength
• Datagridview