Is it possible to cancel adding an item inside BindingSource.AddingNew event handler?
When user clicks on the “Add new” button in my BindingNavigator, I am opening a dialog which may or may not return a valid file. Right now, I have something like this:
void bindingSource_AddingNew(object sender, AddingNewEventArgs e)
{
using (var dialog = new OpenFileDialog())
{
var result = dialog.ShowDialog();
// user canceled?
if (result == DialogResult.Cancel)
return;
// TryLoad will return null on failure
var data = TryLoad(dialog.FileName);
// only add the item to the grid if not null
if (data != null)
e.NewObject = data;
}
}
Even if I don’t set e.NewObject to a value inside the handler, a new (“empty”) item appears in the DataGridView.
Is it possible to cancel adding the item?
A simple solution:
Double click on the plus sign in bindingNavigator to generate Click event for the plus button and manually control add new item procedure, like the following: