在 Visual C# 中修复我的 MS Access 数据库时出错
Error fixing my MS Access Database in Visual C#
我一直在使用 windows 表单为作业创建一个项目,基本上我的程序包括 3 个表单:登录、数据库查看器和一个单独的表单来编辑数据库。
编辑数据库时:如果数据库为空,我尝试删除一条记录两次,我的程序中断并收到错误:System.InvalidOperationException: 'Current item cannot be removed from the list because there is no current item.'
是否有任何类型的代码可以实现以防止在 table 为空时使用删除按钮?
这是我的数据库删除按钮的代码,实际上没有任何其他代码与我自己更改的数据库有关,因为我不知道该怎么做。
private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Are you sure you'd like to delete this record?", "Delete Record", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
this.computer_GamesBindingSource.RemoveCurrent();
this.Validate();
this.computer_GamesBindingSource.EndEdit();
}
}
试试这样检查:
if (this.computer_GamesBindingSource.Count > 0)
{
/* put delete code here */
}
这样只有在有记录的情况下才会执行删除代码
有关详细信息,请参阅 https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.bindingsource.count
我一直在使用 windows 表单为作业创建一个项目,基本上我的程序包括 3 个表单:登录、数据库查看器和一个单独的表单来编辑数据库。
编辑数据库时:如果数据库为空,我尝试删除一条记录两次,我的程序中断并收到错误:System.InvalidOperationException: 'Current item cannot be removed from the list because there is no current item.'
是否有任何类型的代码可以实现以防止在 table 为空时使用删除按钮?
这是我的数据库删除按钮的代码,实际上没有任何其他代码与我自己更改的数据库有关,因为我不知道该怎么做。
private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Are you sure you'd like to delete this record?", "Delete Record", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
this.computer_GamesBindingSource.RemoveCurrent();
this.Validate();
this.computer_GamesBindingSource.EndEdit();
}
}
试试这样检查:
if (this.computer_GamesBindingSource.Count > 0)
{
/* put delete code here */
}
这样只有在有记录的情况下才会执行删除代码
有关详细信息,请参阅 https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.bindingsource.count