我用datagridview绑定了数据表,我打算实现的功能是,如果用户点击了界面右上角的“×”时,提醒用户保存在datagridview中所做的更改。
代码如下:
在FormCloing事件中, 做如下判断:
if (ds.Tables[0].GetChanges() != null)
{
if( MessageBox.Show("数据已经更改,是否要保存?", "确认",
MessageBoxButtons.YesNo,MessageBoxIcon.Question) == DialogResult.Yes )
{
saveBT_Click(sender, e);
}
}
发现ds.Tables[0].GetChanges() 总是null,数据也就不能保存;如果通过"关闭"按钮,做同样的判断,就可以正常保存。
这是为什么呢?
代码如下:
在FormCloing事件中, 做如下判断:
if (ds.Tables[0].GetChanges() != null)
{
if( MessageBox.Show("数据已经更改,是否要保存?", "确认",
MessageBoxButtons.YesNo,MessageBoxIcon.Question) == DialogResult.Yes )
{
saveBT_Click(sender, e);
}
}
发现ds.Tables[0].GetChanges() 总是null,数据也就不能保存;如果通过"关闭"按钮,做同样的判断,就可以正常保存。
这是为什么呢?
BindingSource再赋给表格控件,然后保存之前依次调用:
this.Validate();
BindingSource.EndEdit();