1 Replies - 978 Views - Last Post: 23 January 2014 - 02:27 AM Rate Topic: -----

#1 STP_Captain_Slow  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 08-October 10

Datagridview Combobox selected value changed

Posted 22 January 2014 - 06:16 AM


I am trying to catch when the value of a combobox in a Datagridview is changed and do something based on the new selected value.

I have found and been using code found in post #4 in this link:


I have slightly modified it so it get the new selected value and does something based on it, I also changed a couple things as the code in the above post only seemed to fire on first selection from the combobox, i.e. if I selected a value then selected a different value without leaving the control the second selection wouldn't fire the event.

Below is where I have got the code up to, it fires every time a section is made within the control however when I try to leave the control it goes into an infinite loop within "editingComboBox_SelectedIndexChanged" Any help to fix the issue with my code or a fix to the original code in the link so it will fire each time a value is selected without leaving the control.

Private Sub dgvEmbroList_EditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs) Handles dgvEmbroList.EditingControlShowing

        'Get the Editing Control. I personally prefer Trycast for this as it will not throw an error
        Dim editingComboBox As ComboBox = TryCast(e.Control, ComboBox)
        If Not editingComboBox Is Nothing Then
            'Add the handle to your IndexChanged Event
            varRowIndex = dgvEmbroList.CurrentCell.RowIndex
            AddHandler editingComboBox.SelectedIndexChanged, AddressOf editingComboBox_SelectedIndexChanged
        End If

        'Prevent this event from firing twice, as is normally the case.
        RemoveHandler dgvEmbroList.EditingControlShowing, AddressOf dgvEmbroList_EditingControlShowing

End Sub

Private Sub editingComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        'Get the editing control
        Dim editingComboBox As ComboBox = TryCast(sender, ComboBox)
        If editingComboBox Is Nothing Then Exit Sub

        'Show your Message Boxes
        If editingComboBox.Text = "Part" Then

            Dim dlgEnterQty As New dlgEnterQty
            dlgEnterQty.varStockCode = dgvEmbroList.Rows(varRowIndex).Cells("Stock Code").Value & "-" & dgvEmbroList.Rows(varRowIndex).Cells("Size").Value
            dlgEnterQty.varOutStd = dgvEmbroList.Rows(varRowIndex).Cells("Qty OutStd").Value
            Dim result As DialogResult = dlgEnterQty.ShowDialog(Me)
            If result = Windows.Forms.DialogResult.OK Then
                dgvEmbroList.Rows(varRowIndex).Cells("Qty Embro").Value = dlgEnterQty.EnterQty
                dgvEmbroList.Columns("Qty Embro").Visible = True
                dgvEmbroList.Width = 679
            ElseIf result = Windows.Forms.DialogResult.Cancel Then
                Exit Sub
            End If
            dgvEmbroList.Rows(varRowIndex).Cells("Qty Embro").Value = dgvEmbroList.Rows(varRowIndex).Cells("Qty OutStd").Value
        End If
End Sub

Is This A Good Question/Topic? 0
  • +

Replies To: Datagridview Combobox selected value changed

#2 STP_Captain_Slow  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 08-October 10

Re: Datagridview Combobox selected value changed

Posted 23 January 2014 - 02:27 AM


Noticed quite a few views so guessing other people are struggling on this as well,

I did another search and for me this code worked perfect and fires each time a new value is selected and does allow me to get out of the selected row... (http://stackoverflow.com/questions/9953006/selection-changed-event-in-datagridcomboboxcolumn)

Thanks for looking.

Private Sub DataGridView1_EditingControlShowing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
    If DataGridView1.CurrentCell.ColumnIndex = 1 Then
        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.SelectionchangeCommitted, New EventHandler(AddressOf ComboBox_SelectionchangeCommitted)

            ' Add the event handler. 
            AddHandler combo.SelectionchangeCommitted, New EventHandler(AddressOf ComboBox_SelectionchangeCommitted)
        End If
    End If
End Sub

Private Sub ComboBox_SelectionchangeCommitted(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim combo As ComboBox = CType(sender, ComboBox)
End Sub

This post has been edited by STP_Captain_Slow: 23 January 2014 - 02:29 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1