0

Follow-on from question here : How to assign typical NumericUpDown properties to custom NumericUpDown based control?

I'm trying to do something similar to this scenario but I don't want / need an entire column of NumericUpDown controls; I just need a custom NumericUpDownCell-type class that I can switch in to a specific TextBox cell in a DataGridView, use it for data-entry and then flatten it back to a plain DataGridViewTextBoxCell again.

So I'm using pretty much the exact same code as the example in the linked question (in terms of the classes NumericUpDownCell and NumericUpDownEditingControl) but instead of creating an entire column, I just want to take a single DataGridViewTextBoxCell, dispose it and replace it with a NumericUpDownCell, á la :

Private Sub ConfigureField(activeRow As DataGridViewRow)
    Try
        Dim lowerLimit As Integer = 0
        Dim upperLimit As Integer = 50
        Dim cfgValueCell = dgv(myColumn.Index, activeRow.Index)

        cfgValueCell.Dispose()
        cfgValueCell = New DataGridViewCustomNumericUpDownCell With {
                                .Minimum = lowerLimit,
                                .Maximum = upperLimit}
        dgv(myColumn.Index, activeRow.Index) = cfgValueCell

    Catch ex As Exception

    End Try
End Sub

But that throws an exception in the Public Overrides Sub InitializeEditingControl for the class NumericUpDownCell :

Public Overrides Sub InitializeEditingControl(rowIndex As Integer, initialFormattedValue As Object, dataGridViewCellStyle As DataGridViewCellStyle)
    MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)
    Dim ctl As NumericUpDownEditingControl = CType(DataGridView.EditingControl, NumericUpDownEditingControl)
    ctl.Value = CType(Me.Value, Decimal)
    ctl.Minimum = Me.Minimum
    ctl.Maximum = Me.Maximum
End Sub

Specifically :

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

Am I missing a step somewhere?

Alan O'Brien
  • 151
  • 1
  • 13
  • An empty Try-Catch usually isn't a good thing. – LarsTech Nov 14 '22 at 15:42
  • Yup, do appreciate that @LarsTech, right now I'm just using the Try-Catch to capture exceptions while I'm writing/debugging. For what it's worth, the Try-Catch in the `ConfigureField` sub doesn't throw any exceptions; it's the `InitializeEditingControl` override sub in the custom derived class where the exception occurs (i.e. it seems I can dispose-and-replace the cell in the grid; it's only when I enter the new cell - thus initialise the EditingControl - that the quoted exception occurs?) – Alan O'Brien Nov 14 '22 at 16:17

0 Answers0