locked
Datagridview combo box behaviour RRS feed

  • Question

  • Hi

    I have an unbound datagridview control whose first column is a combo box This contains a list of part numbers.

    I am having some problems with the datagridview automtically adding a new row when the combo box item is selected.

    If I select an entry from the data by typing in part number in full in the first row and pressing tab or enter then I get a new row created. No problems.

    However, if I then START typing a part number in the second row AND click on the part number in the list that is suggested then I DO NOT get a new row.

    However, if, on the first row I START typing a part number AND click on the part number in the list that is suggested I DO get a new row.

    What is happening here? I am slowly going mad trying to work out what is wrong.

     

    My code is as follows:

     

     Private Sub dgOrders_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgOrders.EditingControlShowing
    
        Try
          If TypeOf e.Control Is System.Windows.Forms.ComboBox Then
            With DirectCast(e.Control, System.Windows.Forms.ComboBox)
              cmbItem = CType(e.Control, ComboBox)
              'Set the dropdown style 
              .DropDownStyle = ComboBoxStyle.DropDown
              .AutoCompleteMode = AutoCompleteMode.Suggest
              .AutoCompleteSource = AutoCompleteSource.ListItems
    
              RemoveHandler cmbItem.Validated, AddressOf SelectItemSage
              AddHandler cmbItem.Validated, AddressOf SelectItemSage
    
              RemoveHandler cmbItem.SelectedIndexChanged, AddressOf SelectItem
              AddHandler cmbItem.SelectedIndexChanged, AddressOf SelectItem
            End With
          End If
        Catch ex As Exception
          MsgBox(ex.Message)
        End Try
    
      End Sub
      Private Sub SelectItem(ByVal sender As Object, ByVal e As System.EventArgs)
        mintItemRow = cmbItem.SelectedIndex
    
        ' If it is 0 or greater get the data
        If mintItemRow > -1 Then
          If mblnItem = True Then
            mstrItem = gdstStock.Tables(0).Rows(mintItemRow).Item("Stock_Code").ToString
          Else
            Dim strSQL As String = ""
            ' Set up any Stock fields 
            ' Get the Stock details from our table
    
            Dim strSelect As String = "DESCRIPTION = '" & gdstStock.Tables(0).Rows(mintItemRow).Item("Description").ToString & "'"
            For Each dr As DataRow In gdstStock.Tables(0).Select(strSelect)
              mstrItem = CStr(dr("Stock_Code"))
            Next
          End If
        End If
      End Sub
    

    The routine SelectItemSage just does a lookup on the part number and populates the remaining columns in the datagridview.

    In the first instance where I AM getting a new row, this row appears in the datagridview the moment I press enter or tab after typing in the part number.

    Similarly, If I select an item from the suggest list by clicking on it IN THE FIRST ROW only then the moment I click on it I get a new row.

    However, from then on if I click on any item in the suggest list I DO NOT get a new row.

    I stuck a load of message boxes in the code and the odd thing is that when I type the item number in the first row the select item event does not fire until I have pressed tab or return.

    As soon as I start typing the part number in the second row (on the very first character) the select item event fires three times!

    Help!

     

    Roy

     

     


    Roy T
    Monday, June 27, 2011 9:41 AM

Answers

  • Hi rothco,

    The reason is the changes in the current cell is not committed to the data cache without ending edit mode. Please try to add

    DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)

    to DataGridView1_EditingControlShowing() method. Then it will work fine.

    Please have a try and let us know the results on your side.

     Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
    
      Try
    
       If TypeOf e.Control Is System.Windows.Forms.ComboBox Then
    
        With DirectCast(e.Control, System.Windows.Forms.ComboBox)
    
         cmbItem = CType(e.Control, ComboBox)
    
         'Set the dropdown style 
    
         .DropDownStyle = ComboBoxStyle.DropDown
    
         .AutoCompleteMode = AutoCompleteMode.Suggest
    
         .AutoCompleteSource = AutoCompleteSource.ListItems
    
         RemoveHandler cmbItem.Validated, AddressOf SelectItemSage
    
         AddHandler cmbItem.Validated, AddressOf SelectItemSage
    
         RemoveHandler cmbItem.SelectedIndexChanged, AddressOf SelectItem
    
         AddHandler cmbItem.SelectedIndexChanged, AddressOf SelectItem
    
        End With
    
        DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
    
       End If
    
      Catch ex As Exception
    
       MsgBox(ex.Message)
    
      End Try
    
     End Sub

    Have a nice day.

    Best regards


    Liliane Teng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Marked as answer by rothco Monday, July 4, 2011 3:54 PM
    Monday, July 4, 2011 11:48 AM

All replies

  • Dear Roy T,

    By default if you click tab key when your focus in the Last row (last cell in the grid); it'll add new row to the Datagridview. That's why When you press tab key, its adding new row.

    When focus not in the last row it won't add new row even though we pressing tab key.

    It seems Enter key working as tab key.

    Please read the below thread and let me know if it’s not rectified your problem

    http://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/5ccdcf3b-df84-42a4-92b9-37a9631684a2/

     


    Thanks and Regards, Bharath S.
    Monday, June 27, 2011 1:40 PM
  • Hi Bharath

    Thanks for that. I obviously have not explained myself clearly.

    The focus is in the first row, first column.

    I type a whole part number and press either tab or return.

    The datagridview gives me a new row and (in the case of tab) moves me to the first row, second column.

    I click on the second row, first column.

    I start typing a part number.

    I select my part number from the drop down list that is presented by clicking on the part number in the list.

    I press return or tab

    The datagridview DOES NOT give me a new row

    I have read your link and I think it is a different situation to mine.

     

    Roy


    Roy T
    Monday, June 27, 2011 1:58 PM
  • Sorry - slight correction

    I click on the item in the list and the datagridview does not give me a new row. I do not have to press tab or return

    Roy


    Roy T
    Monday, June 27, 2011 2:00 PM
  • Sorry - slight correction

    I click on the item in the list and the datagridview does not give me a new row. I do not have to press tab or return

    Roy


    Roy T

    Hello rothco,

    Maybe I did not understand this issue on your side very clearly. But on my side, I reproduce the issue as this:

    I create an unbound datagridview control whose first column is a ComboBoxColumn which contains a list of part numbers. When I click on the item in the list in the last row first column every time, the DataGriView will add a new row below. 

    So what do you mean "I click on the item in the list and the datagridview does not give me a new row"? Do you want to add a new row when you click on the item in the list every time? Could you please describe this problem more clearly?

    By the way, I could not reproduce the code you provided on my side, would you mind providing more code?

    If I misunderstood, please feel free to follow up.

    Have a nice day.

    Best regards


    Liliane Teng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, June 28, 2011 8:44 AM
  • Hi Liliane

    Sorry not not being clear.

    I have the unbound datagridview with its first column as a type combobox.

    I have filled the combobox with part numbers.

    The EditingControlShowing event fires when I go into the combobox and, as you can see from my code, sets the combo box to have the property of Autocomplete mode - suggest. This means that when you click on the combo box, or when you start typing in the combo box you get a list shown of all the part numbers in my list.

    This event also sets up a couple of event handlers. One to fire when the combobox selectedindex changes and one to fire when the entry in the combo box is validated.

    The issue is that if I TYPE (as opposed to selecting an item in the suggest list) the whole part number into row one, column one then the moment I press tab the cursor moves to the next column (column 2 , row 1) AND the datagridview gives me a empty row below the one I am working in - which is what I want.

    If I then click on the combobox in Row 2 Column 1 a suggest list appears. If I click on an entry in the suggest list then the entry is store in column 2 row 1. If I now press tab then the cursor moves to the next column (column 2 row 2) and NO new entry appears below the one I am working in - which is NOT what I want. How can I now move my cursor to row 3 column 1 - there is no row 3 !!

    So the problem is arising by typing the whole part number in the first row, followed by selecting an entry from the suggest list in the second row. You should be able to do either and get a new blank row.

    What were the problems with the code? Can you not take it from the code above?

    Thanks again for all your help

    Roy


    Roy T
    Tuesday, June 28, 2011 9:29 AM
  • Hi rothco,

    I could understand this problem from above description. But above code you provided is not complete. Would you mind providing the whole project code or a simple sample which could explains this problem clearly? If so, we could reproduce the issue on our side.

    Have a nice day.

    Best regards


    Liliane Teng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, June 30, 2011 10:25 AM
  • Hi Liliane

    Thanks for that

    I will try and put together a stand alone version of the problem in the next working day or so.

    Is there any way I can post an entire solution here?

    Roy


    Roy T
    Friday, July 1, 2011 8:13 AM
  • Hi rothco,

    You could upload the entire solution to your SkyDrive and share the link to us.

    Best regards


    Liliane Teng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, July 1, 2011 10:24 AM
  • Hi Liliane

    The link is

    https://skydrive.live.com/redir.aspx?cid=a2e28c5d1aefa668&resid=A2E28C5D1AEFA668!118

     

    This is a very simple vb2008 project but it demonstrates the problem. It is called DGTest.

    Run it and TYPE "Test1" into the first column, first row then hit tab. You will get a new datagridview row.

    Now go to Column1 row 2 and select Test2 from the drop down list and click tab. You will NOT get a new row.

    My question is why no new row???

    Thanks again

    Roy

     

     


    Roy T
    Monday, July 4, 2011 8:14 AM
  • Hi rothco,

    The reason is the changes in the current cell is not committed to the data cache without ending edit mode. Please try to add

    DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)

    to DataGridView1_EditingControlShowing() method. Then it will work fine.

    Please have a try and let us know the results on your side.

     Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
    
      Try
    
       If TypeOf e.Control Is System.Windows.Forms.ComboBox Then
    
        With DirectCast(e.Control, System.Windows.Forms.ComboBox)
    
         cmbItem = CType(e.Control, ComboBox)
    
         'Set the dropdown style 
    
         .DropDownStyle = ComboBoxStyle.DropDown
    
         .AutoCompleteMode = AutoCompleteMode.Suggest
    
         .AutoCompleteSource = AutoCompleteSource.ListItems
    
         RemoveHandler cmbItem.Validated, AddressOf SelectItemSage
    
         AddHandler cmbItem.Validated, AddressOf SelectItemSage
    
         RemoveHandler cmbItem.SelectedIndexChanged, AddressOf SelectItem
    
         AddHandler cmbItem.SelectedIndexChanged, AddressOf SelectItem
    
        End With
    
        DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
    
       End If
    
      Catch ex As Exception
    
       MsgBox(ex.Message)
    
      End Try
    
     End Sub

    Have a nice day.

    Best regards


    Liliane Teng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Marked as answer by rothco Monday, July 4, 2011 3:54 PM
    Monday, July 4, 2011 11:48 AM
  • Hi Liliane

    That's it!!

    Many thanks

    Roy


    Roy T
    Monday, July 4, 2011 3:54 PM