我一直在為使用 Windows 表單的作業創建一個專案,基本上我的程式包括 3 個表單:登錄、資料庫查看器和一個用于編輯資料庫的單獨表單。
編輯資料庫時:如果資料庫為空,并且我嘗試洗掉記錄兩次我的程式中斷,我收到錯誤: System.InvalidOperationException: 'Current item cannot be removed from the list because there is no current item.'
是否有任何型別的代碼可以實作以防止在表為空時使用洗掉按鈕?
這是我用于資料庫洗掉按鈕的代碼,實際上沒有任何其他代碼與我自己更改的資料庫有關,因為我不知道該怎么做。
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();
}
}
uj5u.com熱心網友回復:
嘗試做這樣的檢查:
if (this.computer_GamesBindingSource.Count > 0)
{
/* put delete code here */
}
這樣洗掉代碼只有在有記錄時才會執行。
有關詳細資訊,請參閱https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.bindingsource.count
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/382074.html
上一篇:從左到右和從右到左移動標簽
