datagridview listindex for a combobox column
-
Wednesday, August 01, 2012 7:13 PM
Hi All:
I am using vb.Net 2008. I have an unbound datagridview and column 2 is a combobox column. Is there a way for me to get the selectedindex of the combobox? I would appreciate a code. Thanks;
Bob
All Replies
-
Thursday, August 02, 2012 12:16 AM
- Proposed As Answer by Mark Liu-lxfModerator Friday, August 03, 2012 2:01 AM
-
Friday, August 03, 2012 5:12 PM
Thank you Paul. I will look at this in a couple of days as I have to work on other stuff for the next few days.
Bob
-
Friday, August 03, 2012 5:29 PM
Hi,
To get the selected index from selected item of dataGridViewComboBoxCell, you will have to do some "work around" to get it. There is no direct way of getting it.
This is my example code to show you how its done:
Public Sub New() InitializeComponent() dataGridView1.Columns.Add("col1", "column 1") 'some othe columns Dim comboCol As DataGridViewComboBoxColumn = CreateComboBox() dataGridView1.Columns.Add(comboCol)
'adding some example rows to dgv (you can remove this bellow): For i As Integer = 0 To 5 dataGridView1.Rows.Add(i.ToString()) Next End Sub Private Function CreateComboBox() As DataGridViewComboBoxColumn Dim cmbcolumn As New DataGridViewComboBoxColumn() If True Then cmbcolumn.Name = "col2" cmbcolumn.HeaderText = "column 2" cmbcolumn.Items.AddRange(New String() {"aa", "ac", "aacc"}) 'exampe data of comboBox dataGridView1.EditingControlShowing += New DataGridViewEditingControlShowingEventHandler(AddressOf dataGridView1_EditingControlShowing) End If Return cmbcolumn End Function Private Sub dataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Dim combo As ComboBox = TryCast(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. combo.SelectedIndexChanged -= New EventHandler(AddressOf ComboBox_SelectedIndexChanged) ' Add the event handler. combo.SelectedIndexChanged += New EventHandler(AddressOf ComboBox_SelectedIndexChanged) End If End Sub Private Sub ComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Dim cb As ComboBox = DirectCast(sender, ComboBox) Dim index As Integer = cb.SelectedIndex MessageBox.Show("Selected index is " & index) End Sub
Hope it helps,
bye, bye
Mitja
-
Tuesday, August 07, 2012 6:04 PM
Hi Mitja:
I pasted your code in my form, and it would not run. there were compile errors.
Bob
-
Tuesday, August 07, 2012 6:17 PM
Hi Mark:
I looked at your control. How would I get the selectedindex of the combobox item? in the cellSelectedIndexChanged event, I did not see a property under e. for the selectedindex. Thanks.
Bob
-
Tuesday, August 07, 2012 6:35 PM
Hi Mark:
I looked at your control. How would I get the selectedindex of the combobox item? in the cellSelectedIndexChanged event, I did not see a property under e. for the selectedindex. Thanks.
Bob
you mean my control, right?
here it is with a selectedIndex property:
Public Class DataGridViewEx Inherits DataGridView Public Event cellSelectedIndexChanged(ByVal sender As System.Object, ByVal e As ComboIndexChangedEventArgs) Public Event cellCheckedChanged(ByVal sender As System.Object, ByVal e As CheckBoxChangedEventArgs) Public Sub New() End Sub Protected Overrides Sub OnEditingControlShowing(ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) ' Attempt to cast the EditingControl to a ComboBox. 'this will only work if CurrentCell is a combobox column Dim cb As ComboBox = TryCast(e.Control, ComboBox) 'if it is a combobox column... If cb IsNot Nothing Then RemoveHandler cb.SelectedIndexChanged, AddressOf DGVComboIndexChanged AddHandler cb.SelectedIndexChanged, AddressOf DGVComboIndexChanged End If MyBase.OnEditingControlShowing(e) End Sub Private Sub DGVComboIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) 'this handles the datagridviewcombobox cell selectedindexchanged event Dim cb As ComboBox = DirectCast(sender, ComboBox) RaiseEvent cellSelectedIndexChanged(sender, New ComboIndexChangedEventArgs With {.columnIndex = MyBase.CurrentCell.ColumnIndex, .rowIndex = MyBase.CurrentCell.RowIndex, .selectedIndex = cb.SelectedIndex, .selectedItem = cb.SelectedItem, .selectedValue = cb.SelectedValue, .text = cb.Text}) End Sub Protected Overrides Sub OnCurrentCellDirtyStateChanged(ByVal e As System.EventArgs) If TypeOf MyBase.CurrentCell Is DataGridViewCheckBoxCell Then MyBase.CommitEdit(DataGridViewDataErrorContexts.Commit) End If MyBase.OnCurrentCellDirtyStateChanged(e) End Sub Protected Overrides Sub OnCellValueChanged(ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) If e.ColumnIndex = -1 OrElse e.RowIndex = -1 Then Return If TypeOf MyBase.Rows(e.RowIndex).Cells(e.ColumnIndex) Is DataGridViewCheckBoxCell Then RaiseEvent cellCheckedChanged(Me, New CheckBoxChangedEventArgs With {.columnIndex = e.ColumnIndex, .rowIndex = e.RowIndex, .newValue = If(MyBase.Rows(e.RowIndex).Cells(e.ColumnIndex).Value Is Nothing, False, CBool(MyBase.Rows(e.RowIndex).Cells(e.ColumnIndex).Value))}) End If MyBase.OnCellValueChanged(e) End Sub End Class Public Class ComboIndexChangedEventArgs Public columnIndex As Integer Public rowIndex As Integer Public selectedIndex as integer Public selectedItem As Object Public selectedValue As Object Public text As String End Class Public Class CheckBoxChangedEventArgs Public columnIndex As Integer Public rowIndex As Integer Public newValue As Boolean End Class
thanks for any help
- Marked As Answer by booboo_US Thursday, August 09, 2012 3:21 AM
-
Thursday, August 09, 2012 3:22 AMThank you Paul. It works great.

