Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 644
  • Last Modified:

datagridview multiple comboboxdropdowns across row....nasty bug...

My example inventory datagrid has four visible columns:

The first is just a text description, the second is a a Room, the
third is a list of items in the selected room, and the last is the final disposition
of the item in the room (Sell, Keep, or Throw_Away).

Columns Room, Item and Dispostion are datagridcomboboxes.

Items available to pick are determined by the selectedvaluechanged event of the Room
datagridcombobox.   It calls a function UpdateDisplayedItemListForRow().



When adding a new row, the user fills in the description, then picks a room by pressing a

key, in this example the room can be an Office or a Living Room.  So if the user presses

an "O" the Office selection is made and the user presses a tab key to move to the Items

column.   The office can contain a lamp, a desk, or a table.  So on the items column the user presses an L and the Lamp item is displayed.  Finally the user presses the tab key to move to the Disposition column.

This is where I am having trouble. Sometimes it works great, but at other times pressing

the tab key makes the Lamp entry vanish and leaves the Item column blank again!  I cannot

determine any rhyme or reason for this behavior, nor can I figure out where to put
a breakpoint to catch the problem.  (Although it is much less likely to happen when I am

debugging.)

The code is attached.  Can someone please help me figure out what I am doing to cause

this nasty bug and what I can do to squash it?  Please advise.

Thanks in advance.
Private Sub DataGridViewCommands_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridViewCommands.EditingControlShowing

        Dim cbItem As ComboBox
        Dim cbdisp As ComboBox
        Dim cbrm As ComboBox

        Select Case Me.DataGridViewDescriptions.CurrentCell.ColumnIndex
            Case 0 'ROOMS
                cbrm = TryCast(e.Control, ComboBox)
                If cbrm Is Nothing Then
                    Exit Sub
                End If
                cbrm.Name = "ComboBoxRooms"

                RemoveHandler cbrm.SelectedValueChanged, New EventHandler(AddressOf gen_SelectionChanged)
                AddHandler cbrm.SelectedValueChanged, New EventHandler(AddressOf gen_SelectionChanged)
             

            Case 1   'ItemS
                cbItem = TryCast(e.Control, ComboBox)
                If cbItem Is Nothing Then
                    Exit Sub
                End If
                cbItem.Name = "ComboBoxItems"
               
                RemoveHandler cbItem.SelectedValueChanged, New EventHandler(AddressOf gen_SelectionChanged)
                AddHandler cbItem.SelectedValueChanged, New EventHandler(AddressOf gen_SelectionChanged)



            Case 2   'Dispostion
                cbdisp = TryCast(e.Control, ComboBox)
                If cbdisp Is Nothing Then
                    Exit Sub
                End If
                cbdisp.Name = "ComboBoxDisposition"
                
                RemoveHandler cbdisp.SelectedValueChanged, New EventHandler(AddressOf gen_SelectionChanged)
                AddHandler cbdisp.SelectedValueChanged, New EventHandler(AddressOf gen_SelectionChanged)
                
        End Select

    End Sub
    
  
    Public Sub gen_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim cmbBox As ComboBox = DirectCast(sender, ComboBox)
        Dim XC As DescriptionClass = ctrl.bs.Current

        If cmbBox.SelectedItem Is Nothing Then
            Exit Sub
        End If

        
        Select Case Me.DataGridViewDescriptions.CurrentCell.ColumnIndex
            Case 0
                XC.RoomCode = cmbBox.SelectedItem.RoomNo
                If Not ctrl.UpdateDisplayedItemListForRow(lex, XC.RoomCode) Then
                    MsgBox(lex.FullMessage, MsgBoxStyle.Critical, FormVeraMain.Text)
                End If
            Case 1
                XC.ItemCode = cmbBox.SelectedItem.ItemCode
                XC.X10Code = cmbBox.SelectedItem.X10Code
            Case 2
                XC.DispostionType = cmbBox.SelectedValue
                
        End Select

        If Not XC.NewRow Then
            XC.NextAction = "UPDATE"
        Else
            XC.NextAction = "ADD"
        End If

    End Sub


 Public Function UpdateDisplayedItemListForRow(ByRef lex As LastException, Optional ByVal int_roomcode As Integer = 0) As Boolean

        Dim retval As Boolean = True
        Dim dgr As DataGridViewRow = frm.DataGridViewCommands.CurrentRow
        Dim dgrcell As DataGridViewComboBoxCell
        Dim lst As List(Of ItemClass)

        dgrcell = dgr.Cells("Items")
        Dim roomcode As Integer

        If int_roomcode <> 0 Then
            roomcode = int_roomcode
        Else
            roomcode = dgr.Cells("RoomCode").Value
        End If


        If Not GetItems(lst, roomcode, lex) Then
            retval = False
        Else
            dgrcell.DisplayMember = "ItemName"
            dgrcell.ValueMember = "ItemCode"
            dgrcell.DataSource = lst
        End If
        Return retval

    End Function

Open in new window

0
codefinger
Asked:
codefinger
  • 5
  • 2
2 Solutions
 
tomnorraCommented:
sounds like the item is not actually selected in the combobox which would be causing your error. On the key press event of the combobox you could test for Tab key and if it is tab then make sure to set the selected index value to the item intended to be selected. Check and see if this will fix your issue.
0
 
codefingerAuthor Commented:
set the selected index value to the item intended to be selected.

How do I determine, in code, the item intended to be selected if it is not selected yet?


0
 
codefingerAuthor Commented:
I added msgboxes to the keydown and keypress events for the datagrid, then when those failed to activate I tried it for the form, and still no msgboxes popped up!

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
codefingerAuthor Commented:
Finally I added the event myself to the comboboxes but found it did not activate while I was still editing  a cell in the row.

It seems, based on the message boxes I added to the EditingControlShowing event of the datagrid, that the DataGridViewItems.CurrentCell.ColumnIndex does not always change as a result of pressing the TAB key, thus my code is not always affecting the cell I think it should.

Perhaps there is a more accurate way to determine which cell was selected by pressing the TAB key?


0
 
tomnorraCommented:
on the exit of the combo box you should capture the text in the combobox and then do something like the following. cboMyCombo.SelectedIndex = cboMyCombo.FindStringExact(cboMyCombo.Text)
0
 
codefingerAuthor Commented:
I found a solution, finally, though it does not fully explain the problem.

On the UpdateDisplayedItemListForRow, after I populate the comboboxitems, I now set its selectedindex to -1.   If the user sees a choice already in the cell, he/she may tab past the column without making a choice, so the selectionchangecommitted event does not fire at all and the cell clears.  Even if the user makes the choice, (pressing a key), if he/she chooses the same item that is already in the cell, the result is the same...the selectionchangecommitted event does not fire (because nothing was changed) and the cell clears.

Setting the selectedindex to -1 each time I repopulate the comboboxitems solves the problem.

I also found that many times the wrong event was firing, so I separated the events by combobox, then added a check for the name of the combobox.  If the name does not match what is expected within the event, the sub is exited and nothing happens.  So my events no longer fire on the wrong combo box.

0
 
codefingerAuthor Commented:
Following the process after adding the keydown event to the combobox allowed me to debug and see what was happening after the tab key was pressed.  So even though it was not the solution, it did help.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now