Overwriting BindingNavigator AddNew

In a form, I want particular things to happen on the form when the user pressesthe AddNew button in the BindingNavigator.So I want to call my own code inthebindingNavigatorAddNewItem.Click.

I put myBindingSource.AddNew in thebindingNavigatorAddNewItem_Click, followed by additional code. When I click the AddNew button I immediately get an error telling me that one of the columns in the table can't be null.

Joe Stegmansaid"The BindingNavigator AddNew button calls BindingSource.AddNew() - there's nothing more to it." That's what I thought all along.

So why is the data being validated before I enter anything if I make the call to AddNew, but this doesn't happen if the AddNew button calls AddNew itself?

Robert

[1306 byte] By [RobertGreen] at [2007-12-16]
# 1

Contradictions aside, there is a little more to it. The BindingNavigator first calls its own Validate method and if successful, it will call AddNew on it's BindingSource - it looks very similar to the following:


if (this.Validate()) {
if (this.BindingSource != null) {
this.BindingSource.AddNew();
/* Basically does the following */
this.EndInit();
}
}

Joe Stegman
The Windows Forms Team
Microsoft Corp.

This posting is provided "AS IS" with no warranties, and confers no rights.

JoeStegman at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 2
After playing with this a bit more I think that the AddNew button calls the code above by itself, even if you have put additional code in the Click. I have the following:



Private Sub bindingNavigatorAddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bindingNavigatorAddNewItem.Click
LastNameTextBox.Focus()
End Sub

When I click the AddNew I get a new record and the LastName gets the focus.

So I think my initial problem was that the button called AddNew by itself and then I told it to call it again. I thought these buttons were totally overwriteable. Does the Delete button actually delete and then run whatever code you put in it? The Save doesn't appear to work that way. There is code spit in it that Validates and Updates.

Thanks,
Robert

RobertGreen at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 3

You need to dis-connect the AddNew item from the BindingNavigator. To do this, click on the BindingNavigator and set the "AddNewItem" to (none) in the PropertyGrid.

Joe Stegman
The Windows Forms Team
Microsoft Corp.

This posting is provided "AS IS" with no warranties, and confers no rights.

JoeStegman at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 4
Does the same apply to DeleteItem? If you put code in the DeleteItem_Click to delete, are you calling the generated Delete code twice?

Robert

RobertGreen at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 5

Yes - if you want to replace the default handling, you need to detach the item from the BindingNavigator.

Joe Stegman
The Windows Forms Team
Microsoft Corp.

This posting is provided "AS IS" with no warranties, and confers no rights.

JoeStegman at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...