|
-
Jul 28th, 2011, 02:05 AM
#1
Thread Starter
Member
[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
-
Jul 28th, 2011, 05:53 AM
#2
Re: Datagridview Combobox SelectedIndexChanged
You need to include the RemoveHandler statement and it needs to be before the AddHandler statement.
-
Jul 28th, 2011, 06:35 AM
#3
Thread Starter
Member
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
-
Jul 28th, 2011, 09:22 AM
#4
Hyperactive Member
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:
Public Class Form1
Private Sub dgvDeparture_EditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs) Handles dgvDeparture.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
AddHandler editingComboBox.SelectedIndexChanged, AddressOf editingComboBox_SelectedIndexChanged
End If
'Prevent this event from firing twice, as is normally the case.
RemoveHandler dgvDeparture.EditingControlShowing, AddressOf dgvDeparture_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
MessageBox.Show(editingComboBox.SelectedIndex.ToString()) ' Display index
MessageBox.Show(editingComboBox.Text) ' Display value
'Remove the handle to this event. It will be readded each time a new combobox selection causes the EditingControlShowing Event to fire
RemoveHandler editingComboBox.SelectedIndexChanged, AddressOf editingComboBox_SelectedIndexChanged
'Re-enable the EditingControlShowing event so the above can take place.
AddHandler dgvDeparture.EditingControlShowing, AddressOf dgvDeparture_EditingControlShowing
End Sub
End Class
Hope this helps
-
Jul 28th, 2011, 10:55 AM
#5
Thread Starter
Member
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!!!
-
Jul 28th, 2011, 12:21 PM
#6
Hyperactive Member
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:
RemoveHandler dgvDeparture.EditingControlShowing, AddressOf dgvDeparture_EditingControlShowing
In the SelectedIndexChanged Event:
VB.NET Code:
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.
-
Jul 28th, 2011, 12:36 PM
#7
Thread Starter
Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|