views:

520

answers:

2

Simple problem (I think): I want to be able to invoke a click method on a predefined object, specifically, the bindingNavigatorDeleteItem button on the standard c# BindingNavigator. I need to intercept the delete so that I can verify that the record is allowed to be deleted. If it is, I want to invoke the aforementioned click event which does a nice job of deleting said record. If the record is not eligible for deletion, I want to abort the delete.

An engineering colleague of mine suggests that I simply add another button to the toolstrip and use it's click method (which, of course, I can get to) to check the records eligibility and call the original delete button as needed.

If there is another, better way, please pass it along.

+2  A: 

Instead of intercepting the toolstrip button (and leaving other techniques open to the user, such as deleting a row within a DataGrid), use one of the events on your Data Source to cancel the delete if invalid.

Bevan
I had thought of that but when I examined the RowDeleting event, I did not see an obvious way of terminating the delete (like a Cancel in the CancelEventArgs of some events) so I went down a different track. If I missed something, please illuminate!
Bruce
You haven't missed anything - the DataTable.RowDeleting event lacks a way to cancel the delete. A shame, as it forces you into workarounds.
Bevan
+1  A: 

To simply invoke the click - you should be able to use PerformClick().

I was a little lost by the other things you mentioned - can you clarify?

Marc Gravell
I tried the "PerformClick()" method and...nothing happens! Is there some other sort of preparation I must do before the PerformClick() method actually performs? The help that comes with the method is not what you would call "informative" - it's a single line of text that says"Activates the toolstrip item when it is clicked with the mouse" - as I say, not real helpful. I must be missing something as I have tried, successfully, to call the method via Reflection (thanks to another posting in StackOverflow) but the elegance of PerformClick() is what I am looking for. No worries on the other stuff.
Bruce
Never mind. The "brilliant" programmers at MS decided that if a button wasn't visible, it's functionality wasn't needed so...PerformClick works only when the button is both enabled and visible (got that from the source code) - go figure!
Bruce