troubleshooting Question

datagrid and to avoid error when edit control is NOT combobox?

Avatar of codefinger
codefingerFlag for United States of America asked on
Visual Basic.NET
2 Comments1 Solution1093 ViewsLast Modified:
The code attached ALMOST works, but when the edit control is a bound textbox on the same row, an error occurs...

Unable to cast object of type 'System.Windows.Forms.DataGridViewTextBoxEditingControl' to type 'System.Windows.Forms.ComboBox'.

How do I avoid activating the code in EditingControlShowing when I don't need it or want it?

Thanks in advance.
Private Sub DataGridViewForms_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridViewForms.EditingControlShowing
        Dim editingComboBox As ComboBox = CType(e.Control, ComboBox)
        If Not editingComboBox Is Nothing Then
            AddHandler editingComboBox.SelectedIndexChanged, AddressOf editingComboBox_SelectedIndexChanged
        End If
    End Sub

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

        Dim comboBox1 As ComboBox = CType(sender, ComboBox)
        Dim dgr As System.Windows.Forms.DataGridViewRow = Nothing
        Dim fc As HPFFAFormClass = Nothing

        comboBox1.DisplayMember = "Description"
        comboBox1.ValueMember = "HPFDocType"

        dgr = Me.DataGridViewForms.CurrentRow
        fc = dgr.DataBoundItem
        fc.HPFDocType = comboBox1.SelectedValue

    End Sub

Open in new window


Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros