How to determine if DataGridView contains uncommitted changes when bound to a SqlDataAdapter

Posted by JYelton on Stack Overflow See other posts from Stack Overflow or by JYelton
Published on 2010-05-11T16:31:53Z Indexed on 2010/05/11 16:34 UTC
Read the original article Hit count: 495

Filed under:
|
|

I have a form which contains a DataGridView, a BindingSource, a DataTable, and a SqlDataAdapter. I populate the grid and data bindings as follows:

private BindingSource bindingSource = new BindingSource();
private DataTable table = new DataTable();
private SqlDataAdapter dataAdapter = new SqlDataAdapter("SELECT * FROM table ORDER BY id ASC;", ClassSql.SqlConn());
private void LoadData()
{
    table.Clear();
    dataGridView1.AutoGenerateColumns = false;
    dataGridView1.DataSource = bindingSource;
    SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
    table.Locale = System.Globalization.CultureInfo.InvariantCulture;
    dataAdapter.Fill(table);
    bindingSource.DataSource = table;
}

The user can then make changes to the data, and commit those changes or discard them by clicking either a save or cancel button, respectively.

private void btnSave_Click(object sender, EventArgs e)
{
    // save everything to the displays table
    dataAdapter.Update(table);
}

private void btnCancel_Click(object sender, EventArgs e)
{
    // alert user if unsaved changes, otherwise close form
}

I would like to add a dialog if cancel is clicked that warns the user of unsaved changes, if unsaved changes exist.

Question:

How can I determine if the user has modified data in the DataGridView but not committed it to the database? Is there an easy way to compare the current DataGridView data to the last-retrieved query? (Note that there would not be any other threads or users altering data in SQL at the same time.)

© Stack Overflow or respective owner

Related posts about c#

Related posts about winforms