当用户尝试关闭应用程序并且他没有在datagridview的datatable中保存更改时,我试图显示一个消息框。但是当应用关闭时,我的代码中出现错误: System.NullReferenceException:'对象引用未设置为的实例 宾语。' dt为空。
我为此功能编写的代码为:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DataTable changes = dt.GetChanges();
if (changes.Rows.Count > 0)
{
var window = MessageBox.Show(
"You did not save changes! Would you like to return and save
changes made?",
"WARNING!",
MessageBoxButtons.YesNo);
e.Cancel = (window == DialogResult.Yes);
}
}
有什么想法吗?