codefinger
asked on
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 UpdateDisplayedItemListFor Row().
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.
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 UpdateDisplayedItemListFor
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
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.
ASKER
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?
How do I determine, in code, the item intended to be selected if it is not selected yet?
ASKER
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!
ASKER
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.CurrentC ell.Column Index 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?
It seems, based on the message boxes I added to the EditingControlShowing event of the datagrid, that the DataGridViewItems.CurrentC
Perhaps there is a more accurate way to determine which cell was selected by pressing the TAB key?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.