Binding Navigator Tool.Enabled not Working As Expected

I have a binding navigator for which I want toselectively enable/disable the Delete button based on the return value of a table adapter query (given data condition).

When all my variables were declared and instantiated within the procedure enabling/disabling the Delete button it worked. Once I refactored out to having this event update class variables, it all got hosed.

Any ideas on how I can get this functional?

Here's what I do (variables are declared at the class level and assigned values 1) at Form_Load (which works) and 2) at BindingSource_PostionChanged event:



PublicClass ProcessingBatchesForm

Dim currentRowPositionAsInteger = 0
Dim currentRowAs My.DataSet.ProcessingBatchesRow
Dim currentBatchIdAsInteger = 0
Dim batchAlreadyProcessedAsString = ""
Dim
MyDataSetMethodsAsNew MyDataSetMethods

PrivateSub ProcessingBatchesForm_Load(ByVal senderAs System.Object,ByVal eAs System.EventArgs)HandlesMyBase.Load

' Loads data into the 'MyDataSet.ProcessingBatches' table.
Me
.ProcessingBatchesTableAdapter.Fill
(
Me.MyDataSet.ProcessingBatches)

currentRowPosition =Me.ProcessingBatchesBindingSource.Position
currentRow = _
CType(Me.MyDataSet.ProcessingBatches.Rows(currentRowPosition), _
My.MyDataSet.ProcessingBatchesRow)
currentBatchId = currentRow.BatchId

batchAlreadyProcessed = ""
ConfigureDeleteEnabledOrDisabled()

EndSub

PrivateSub bindingNavigatorDeleteItem_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles bindingNavigatorDeleteItem.Click

batchAlreadyProcessed = ""

' Test to determine if current Batch has already been processed
MyDataSetMethods.GetBatchAlreadyProcessed (currentBatchId, batchAlreadyProcessed)

If batchAlreadyProcessed = "False"Then
' Current batch NOT already processed - process delete per normal means
Else' batchAlreadyProcessed = "True"
' DO NOT delete - return to window without processing
MessageBox.Show( _
"Unable to delete batch. Create new batch should changes be required.", _
"Processing Batch Previously Processed", _
MessageBoxButtons.OK, MessageBoxIcon.Stop, _
MessageBoxDefaultButton.Button1)
Me.ProcessingBatchesBindingSource.CancelEdit()
EndIf

EndSub

PrivateSub ConfigureDeleteEnabledOrDisabled()

batchAlreadyProcessed = ""
' Test to determine if current Batch has already been processed
MyDataSetMethods.GetBatchAlreadyProcessed _
(currentBatchId, batchAlreadyProcessed)

If batchAlreadyProcessed = "False"Then
' Current batch NOT already processed - enable delete tool
Me.ProcessingBatchesBindingNavigator.DeleteItem.Enabled =True
Me.ProcessingBatchesBindingNavigator.Refresh()
Else' batchAlreadyProcessed = "True"
' DO NOT enable delete tool
Me.ProcessingBatchesBindingNavigator.DeleteItem.Enabled =False
Me.ProcessingBatchesBindingNavigator.Refresh()
EndIf

EndSub

PrivateSub ProcessingBatchesBindingSource_PositionChanged(ByVal senderAsObject,ByVal eAs System.EventArgs)Handles ProcessingBatchesBindingSource.PositionChanged

currentRowPosition =Me.ProcessingBatchesBindingSource.Position
currentRow = _
CType(Me.MyDataSet.ProcessingBatches.Rows(currentRowPosition), _
My.MyDataSet.ProcessingBatchesRow)
currentBatchId = currentRow.BatchId
ConfigureDeleteEnabledOrDisabled()

EndSub

EndClass

[9540 byte] By [JustJoe] at [2007-12-16]
# 1

The BindingNavigator will auto-disable the Delete button if there are no items in the list or if "AllowDelete" is set to false on its BindingSource. The BindingNavigator always resets the state as a result of a ListChanged event from the BindingSource. What you are seeing is the BindingNavigator is trying to set the Delete button state based on the current state of the list - and this is happening after you've set the state. There are a couple of ways you can solve this:

1) You can handle the BindingNavigator.RefreshItems event and reset the state there if it's set incorrectly (this is called anytime the BindingNavigator changes button states).

2) You can detach the "Delete" button from the BindingNavigator and do your own enabled/disabled handling. To detach, click on the BindingNavigator and set DeleteItem to "None". You'll then need to double click on the Delete button on the Navigator and add your own "delete" handling as well as write code to handle enabled/disabled state handling.

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

First option blows-up with "There is no row at position -1".
Guess this is being called before the form_load where the table adapter gets filled.

Any ways to overcome this situation.


Private Sub ProcessingBatchesBindingNavigator_RefreshItems(ByVal sender As Object, ByVal e As System.EventArgs) Handles ProcessingBatchesBindingNavigator.RefreshItems

currentRowPosition = Me.ProcessingBatchesBindingSource.Position
currentRow = _
CType(Me.Stellar401kDataSet.ProcessingBatches.Rows(currentRowPosition), Stellar401k.Stellar401kDataSet.ProcessingBatchesRow)
currentBatchId = currentRow.BatchId
ConfigureDeleteEnabledOrDisabled()

End Sub

JustJoe at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 3
Solved by wrapping in

If currentRowPosition > 0 Then


This event fires quite a bit. Is there any documentation on when I can expect this event to fire? Would it make more sense to wire this event post form load and could anyone give a VB.NET sample of how to do this?

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

Yes - it is chatty. It happens anytime the BindingSource.ListChanged event is fired (which may be multiple times in response to any change in the underlying list).

Yet another option, and one that's less chatty, is to hook the EnabledChanged event on the DeleteItem and reset the state if it set incorrectly.

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...