views:

1664

answers:

4

Hi there.

I have a DGV in VB.Net 2008 connected to an Access DB table. The DGV is not Read Only, but is full of read-only columns except for one, which contains a combo box. The combo box allows the user to select an outcome for that particular row, and then the program copies in a pre calculated value into the "Profit" column depending upon the item selected in the combobox. Then the user hits the Save button and the DB updates (currently via SQL methods in the XSD).

Easy enough so far.

Here is the code.

Private Sub DGUserBets_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DGUserBets.EditingControlShowing

    Dim combo As ComboBox = CType(e.Control, ComboBox)

    If (combo IsNot Nothing) Then

         // Remove an existing event-handler, if present, to avoid 
         // adding multiple handlers when the editing control is reused.
        RemoveHandler combo.SelectedIndexChanged, _
            New EventHandler(AddressOf DGUBStake_SelectedIndexChanged)

        // Add the event handler. 
        AddHandler combo.SelectedIndexChanged, _
            New EventHandler(AddressOf DGUBStake_SelectedIndexChanged)

    End If

End Sub


Private Sub DGUBStake_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)

    Dim myStatus As ComboBox = CType(sender, ComboBox)

    Dim row = DGUserBets.CurrentRow

    Select Case myStatus.SelectedIndex
        Case 0
            row.Cells("DGUBProfit").Value = 0
            // pending. no action
        Case 1
            row.Cells("DGUBProfit").Value = row.Cells("DGUBIfWin").Value
            // win
        Case 2
            // loses
            row.Cells("DGUBProfit").Value = row.Cells("DGUBIfLose").Value
        Case 3
            // void
            row.Cells("DGUBProfit").Value = 0
    End Select


End Sub

The problem I have is that it would seem that if a user selects the desired outcome from the combobox but does NOT hit Enter, and simply mouses on to a different combobox to again select the outcome for a different row, the first eventhandler is not disconnected and thus the events fire multiple times. This then causes various default MsgBox errors and brings up problems when the user tries to commit all changes to the DB/exit program etc etc.

What do I need to do? Do I need to .EndEdit somewhere appropriate to force the row to save the changes? And where should I call this?

Thank you.

+1  A: 

A quick glance at the code brings up this question: If you create a new EventHandler when removing the existing one is it the same one?

Sani Huttunen
Hmm.. the EditControlShowing code is from MSDN. Do you think I might not be implementing it correctly?http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxeditingcontrol.aspx
When you use AddHandler you don't save the created EventHandler anywhere. Do that and use that reference when calling RemoveHandler.
Sani Huttunen
On the other hand: You shouldn't even need to use New EvenHandler:RemoveHandler combo.SelectedIndexChanged, AddressOf DGUBStake_SelectedIndexChangedAddHandler combo.SelectedIndexChanged, AddressOf DGUBStake_SelectedIndexChangedshould do.
Sani Huttunen
+1  A: 

I have had a similar issue, add a handler for CellLeave if the cell being exited is the cell you are looking for (IE e.ColumnIndex = myEditableColumn.Index) then call gv.EndEdit()

Also I would recommend making the handlers member variables for assignment and removal because it seems nicer then always saying Remove New and Add New.

Quintin Robinson
Ahh I see!! I have now saved the Handler variable with the code I have posted below. Can you give me a little more clues with the CellLeave handler please?? Thanks!
Sorry - I misread this at first and now see what you mean, I just need to add the code in the DGV's _CellLeave don't I? thanks.
Sorry I got distracted for a while, Yes if you add a handler in the DGVs CellLeave and then you can handle when it is the particular column you are trying to enforce changes.. when it hits and meets the condition call EndEdit on the DGV and it should force the changes.
Quintin Robinson
A: 

CKRet/Quintin , thank you for the fast responses.

A quick try with this code seems better and breakpointing and stepping through the code seems to be firing the events correctly. I'm fairly new to .NET as the last real VB programming I did was VB6 so I'm not sure if this is the most elegant way to solve the problem.

Also a note that when LastEventHandler = Nothing, calling the RemoveHandler does not throw an exception, which is quite nice.

Maybe I should suggest to MS they should update that article.

Private Sub DGUserBets_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DGUserBets.EditingControlShowing

    Dim combo As ComboBox = CType(e.Control, ComboBox)

    Static LastEventHandler As EventHandler

    If (combo IsNot Nothing) Then

        // Remove an existing event-handler, if present, to avoid 
        // adding multiple handlers when the editing control is reused.
        RemoveHandler combo.SelectedIndexChanged, _
            LastEventHandler

        LastEventHandler = New EventHandler(AddressOf DGUBStake_SelectedIndexChanged)

        // Add the event handler. 
        AddHandler combo.SelectedIndexChanged, _
            LastEventHandler

    End If


End Sub
Have you tried my other suggestion not using New EventHandler at all?
Sani Huttunen
Ooh yes I have given that a go and indeed that also works well; I think I will use that as it is simpler code!THANK YOU!!
A: 

Simpler code which also appears to work well, as suggested by CKRet:

    Dim combo As ComboBox = CType(e.Control, ComboBox)

    If (combo IsNot Nothing) Then

       RemoveHandler combo.SelectedIndexChanged, AddressOf DGUBStake_SelectedIndexChanged

       AddHandler combo.SelectedIndexChanged, AddressOf DGUBStake_SelectedIndexChanged

    End If