Custom DataGridView NumericUpDown editing control

159 Views Asked by At

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?

0

There are 0 best solutions below