Solved

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

Posted on 2010-11-07
7
619 Views
Last Modified: 2012-05-10
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
Comment
Question by:codefinger
  • 5
  • 2
7 Comments
 
LVL 3

Expert Comment

by:tomnorra
ID: 34085640
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
 

Author Comment

by:codefinger
ID: 34089272
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
 

Author Comment

by:codefinger
ID: 34089320
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:codefinger
ID: 34090500
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
 
LVL 3

Assisted Solution

by:tomnorra
tomnorra earned 200 total points
ID: 34094337
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
 

Accepted Solution

by:
codefinger earned 0 total points
ID: 34102894
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
 

Author Closing Comment

by:codefinger
ID: 34134594
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 Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now