Results 1 to 7 of 7

Thread: [RESOLVED] Datagridview Combobox SelectedIndexChanged

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    43

    Resolved [RESOLVED] Datagridview Combobox SelectedIndexChanged

    Ive got an example from the web regarding catching the indexchange of a datagridview combobox column. My datagridview is unbound, has got one combobox column and could have multiple rows. When i implement the code below, the indexchange fires even upon selection or when a datagridview combobox from another row is selected.This fires even before actually selecting from the dropdown list of the combobox itself... it will show the initial index and text of the combobox upon focus even before you could select from the pull down... hope somebody could help me... thanks

    Code:
    Private Sub dgvDeparture_EditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs) Handles dgvDeparture.EditingControlShowing
    
                   Dim editingComboBox As ComboBox = CType(e.Control, ComboBox)
            If Not editingComboBox Is Nothing Then
                AddHandler editingComboBox.SelectedIndexChanged, AddressOf editingComboBox_SelectedIndexChanged
            End If
            'RemoveHandler editingComboBox.SelectedIndexChanged, _
            'New EventHandler(AddressOf editingComboBox_SelectedIndexChanged)
        End Sub
        Private Sub editingComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    
            Dim cboCell As ComboBox = CType(sender, ComboBox)
            MessageBox.Show(cboCell.SelectedIndex.ToString()) ' Display index
            MessageBox.Show(cboCell.Text) ' Display value
    
        End Sub
    Last edited by pidyok; Jul 28th, 2011 at 02:41 AM. Reason: addt'l description

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Datagridview Combobox SelectedIndexChanged

    You need to include the RemoveHandler statement and it needs to be before the AddHandler statement.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    43

    Re: Datagridview Combobox SelectedIndexChanged

    I have done it as well as seen on the code below... but still, upon selection/clicking the other combobox of other row(s) on the datagridview, selectedindexchanged is fired already even before the pulldown list is being shown... indexchange is fired once the control or combobox even receives focus only... hoping somebody could shed light... thnks

    Code:
    If Not editingComboBox Is Nothing Then
            RemoveHandler editingComboBox.SelectedIndexChanged, _
            New EventHandler(AddressOf editingComboBox_SelectedIndexChanged)
            AddHandler editingComboBox.SelectedIndexChanged, AddressOf editingComboBox_SelectedIndexChanged
            End If

  4. #4
    Hyperactive Member
    Join Date
    Apr 2011
    Location
    England
    Posts
    421

    Re: Datagridview Combobox SelectedIndexChanged

    Hi pidyok, the EditingControlShowing Event fires twice:

    -When the control gets focus.
    -When a selection within the control takes place.

    The event handles are the key and here's one way you can get the events to fire as needed:
    VB.NET Code:
    1. Public Class Form1
    2.  
    3.     Private Sub dgvDeparture_EditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs) Handles dgvDeparture.EditingControlShowing
    4.  
    5.         'Get the Editing Control. I personally prefer Trycast for this as it will not throw an error
    6.         Dim editingComboBox As ComboBox = TryCast(e.Control, ComboBox)
    7.         If Not editingComboBox Is Nothing Then
    8.             'Add the handle to your IndexChanged Event
    9.             AddHandler editingComboBox.SelectedIndexChanged, AddressOf editingComboBox_SelectedIndexChanged
    10.         End If
    11.  
    12.         'Prevent this event from firing twice, as is normally the case.
    13.         RemoveHandler dgvDeparture.EditingControlShowing, AddressOf dgvDeparture_EditingControlShowing
    14.  
    15.     End Sub
    16.     Private Sub editingComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    17.  
    18.         'Get the editing control
    19.         Dim editingComboBox As ComboBox = TryCast(sender, ComboBox)
    20.         If editingComboBox Is Nothing Then Exit Sub
    21.  
    22.         'Show your Message Boxes
    23.         MessageBox.Show(editingComboBox.SelectedIndex.ToString()) ' Display index
    24.         MessageBox.Show(editingComboBox.Text) ' Display value
    25.  
    26.         'Remove the handle to this event. It will be readded each time a new combobox selection causes the EditingControlShowing Event to fire
    27.         RemoveHandler editingComboBox.SelectedIndexChanged, AddressOf editingComboBox_SelectedIndexChanged
    28.         'Re-enable the EditingControlShowing event so the above can take place.
    29.         AddHandler dgvDeparture.EditingControlShowing, AddressOf dgvDeparture_EditingControlShowing
    30.  
    31.     End Sub
    32.  
    33. End Class
    Hope this helps

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    43

    Re: Datagridview Combobox SelectedIndexChanged

    thanks jay... works great... however, if datagridview's editmode property is set to EditOnKeystokeOrF2, it takes at least 2~4 mouse clicks before the pulldown/dropdown list shows up... if the editmode property is set to EditOnEnter, nothing happens... if you could please again provide a workaround... thanks!!!

  6. #6
    Hyperactive Member
    Join Date
    Apr 2011
    Location
    England
    Posts
    421

    Re: Datagridview Combobox SelectedIndexChanged

    Which Edit Mode will you be using/prefer to use?

    If you would like to use EditOnEnter then you can remove the following two lines:

    In the EditingControlShowing event:
    VB.NET Code:
    1. RemoveHandler dgvDeparture.EditingControlShowing, AddressOf dgvDeparture_EditingControlShowing
    In the SelectedIndexChanged Event:
    VB.NET Code:
    1. AddHandler dgvDeparture.EditingControlShowing, AddressOf dgvDeparture_EditingControlShowing
    In fact you can remove both of those lines in any case and still get the same effect. The main reason for them is to prevent adding and removing the SelectedIndexChanged handler upto 4 times
    Last edited by JayJayson; Jul 28th, 2011 at 12:28 PM.

  7. #7

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    43

    Thumbs up Re: Datagridview Combobox SelectedIndexChanged



    you're a savior...

    thank you very much... thanks again dude...



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width