【发布时间】:2011-02-18 05:50:39
【问题描述】:
我有一个包含 DataGridView、BindingSource、DataTable 和 SqlDataAdapter 的表单。我按如下方式填充网格和数据绑定:
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;
}
然后,用户可以对数据进行更改,并通过分别单击保存或取消按钮来提交或放弃这些更改。
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
}
如果存在未保存的更改,我想在单击取消时添加一个对话框,警告用户未保存的更改。
问题:
如何确定用户是否修改了 DataGridView 中的数据但未将其提交到数据库?有没有一种简单的方法可以将当前 DataGridView 数据与上次检索的查询进行比较? (请注意,不会有任何其他线程或用户同时更改 SQL 中的数据。)
【问题讨论】: