我有一个包含DataGridView,BindingSource,DataTable和SqlDataAdapter的表单。我填充网格和数据绑定如下:如何确定DataGridView是否包含绑定到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数据。)
这可能会成为问题,如果UI变得更加复杂(每个表更多的控件)。 – 2010-05-12 15:31:11