how i can set NumericUpDown Control to DataGridView

tbob
tbob used Ask the Experts™
on
I have only digital data in grid .How i can set NumericUpDown to cell in datagridview?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Hi tbob,

Unless it has been added into Silverlight 3, I am not aware of the DataGridView control being part of WPF or Silverlight.

Are you talking about the System.Windows.Controls.Data.DataGrid Control in Silverlight?

Or the .NET 2.0 winforms System.Windows.Forms.DataGridView control (being used through a WindowsFormsHost in WPF)?

Please let me know so I can come up with a solution to your problem

Tom  

Author

Commented:
hi Tom 1
I used System.Windows.Form.DataGridView control
If you are using System.Windows.Forms.DataGridView through the WindowsFormsHost, then you aren't actually working with WPF or Silverlight, you are working with a Windows Forms .NET Framework 2.0 control.

Someone has posted a solution here: http://social.microsoft.com/Forums/en-US/onecareupdate/thread/297455ff-0484-4dd6-9627-ca1b0e30b015

but I have added their classes below with can be implemented with the following usage (also from the msdn VB forums):

Dim c As New NumericUpDownColumn
c.HeaderText = "Display Name"
c.DataPropertyName = "DataTable Column Name"
Me.DataGridView_Test.Columns.Add(c)


    Public Class NumericUpDownColumn
        Inherits DataGridViewColumn
 
        Public Sub New()
            MyBase.New(New NumericUpDownCell())
        End Sub
 
        Public Overrides Property CellTemplate() As DataGridViewCell
            Get
                Return MyBase.CellTemplate
            End Get
            Set(ByVal value As DataGridViewCell)
 
                ' Ensure that the cell used for the template is a CalendarCell.
                If Not (value Is Nothing) AndAlso _
                    Not value.GetType().IsAssignableFrom(GetType(NumericUpDownCell)) _
                    Then
                    Throw New InvalidCastException("Must be a CalendarCell")
                End If
                MyBase.CellTemplate = value
 
            End Set
        End Property
 
    End Class
 
    Public Class NumericUpDownCell
        Inherits DataGridViewTextBoxCell
 
        Public Sub New()
            ' Use the short date format.
            Me.Style.Format = "#.##"
        End Sub
 
        Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, _
            ByVal initialFormattedValue As Object, _
            ByVal dataGridViewCellStyle As DataGridViewCellStyle)
 
            ' Set the value of the editing control to the current cell value.
            MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, _
                dataGridViewCellStyle)
 
            Dim ctl As NumericUpDownEditingControl = _
                CType(DataGridView.EditingControl, NumericUpDownEditingControl)
            ctl.Value = CType(Me.Value, Decimal)
 
        End Sub
 
        Public Overrides ReadOnly Property EditType() As Type
            Get
                ' Return the type of the editing contol that CalendarCell uses.
                Return GetType(NumericUpDownEditingControl)
            End Get
        End Property
 
        Public Overrides ReadOnly Property ValueType() As Type
            Get
                ' Return the type of the value that CalendarCell contains.
                Return GetType(Decimal)
            End Get
        End Property
 
        Public Overrides ReadOnly Property DefaultNewRowValue() As Object
            Get
                ' Use the current date and time as the default value.
                Return 0
            End Get
        End Property
 
    End Class
 
    Class NumericUpDownEditingControl
        Inherits NumericUpDown
        Implements IDataGridViewEditingControl
 
        Private dataGridViewControl As DataGridView
        Private valueIsChanged As Boolean = False
        Private rowIndexNum As Integer
 
        Public Sub New()
            Me.DecimalPlaces = 2
        End Sub
 
        Public Property EditingControlFormattedValue() As Object _
            Implements IDataGridViewEditingControl.EditingControlFormattedValue
 
            Get
                Return Me.Value.ToString("#.##")
            End Get
 
            Set(ByVal value As Object)
                If TypeOf value Is Decimal Then
                    Me.Value = Decimal.Parse(value)
                End If
            End Set
 
        End Property
 
        Public Function GetEditingControlFormattedValue(ByVal context _
            As DataGridViewDataErrorContexts) As Object _
            Implements IDataGridViewEditingControl.GetEditingControlFormattedValue
 
            Return Me.Value.ToString("#.##")
 
        End Function
 
        Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As  _
            DataGridViewCellStyle) _
            Implements IDataGridViewEditingControl.ApplyCellStyleToEditingControl
 
            Me.Font = dataGridViewCellStyle.Font
            Me.ForeColor = dataGridViewCellStyle.ForeColor
            Me.BackColor = dataGridViewCellStyle.BackColor
 
        End Sub
 
        Public Property EditingControlRowIndex() As Integer _
            Implements IDataGridViewEditingControl.EditingControlRowIndex
 
            Get
                Return rowIndexNum
            End Get
            Set(ByVal value As Integer)
                rowIndexNum = value
            End Set
 
        End Property
 
        Public Function EditingControlWantsInputKey(ByVal key As Keys, _
            ByVal dataGridViewWantsInputKey As Boolean) As Boolean _
            Implements IDataGridViewEditingControl.EditingControlWantsInputKey
 
            ' Let the DateTimePicker handle the keys listed.
            Select Case key And Keys.KeyCode
                Case Keys.Left, Keys.Up, Keys.Down, Keys.Right, _
                    Keys.Home, Keys.End, Keys.PageDown, Keys.PageUp
 
                    Return True
 
                Case Else
                    Return False
            End Select
 
        End Function
 
        Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) _
            Implements IDataGridViewEditingControl.PrepareEditingControlForEdit
 
            ' No preparation needs to be done.
 
        End Sub
 
        Public ReadOnly Property RepositionEditingControlOnValueChange() _
            As Boolean Implements _
            IDataGridViewEditingControl.RepositionEditingControlOnValueChange
 
            Get
                Return False
            End Get
 
        End Property
 
        Public Property EditingControlDataGridView() As DataGridView _
            Implements IDataGridViewEditingControl.EditingControlDataGridView
 
            Get
                Return dataGridViewControl
            End Get
            Set(ByVal value As DataGridView)
                dataGridViewControl = value
            End Set
 
        End Property
 
        Public Property EditingControlValueChanged() As Boolean _
            Implements IDataGridViewEditingControl.EditingControlValueChanged
 
            Get
                Return valueIsChanged
            End Get
            Set(ByVal value As Boolean)
                valueIsChanged = value
            End Set
 
        End Property
 
        Public ReadOnly Property EditingControlCursor() As System.Windows.Forms.Cursor _
            Implements IDataGridViewEditingControl.EditingPanelCursor
 
            Get
                Return MyBase.Cursor
            End Get
 
        End Property
 
        Protected Overrides Sub OnValueChanged(ByVal eventargs As EventArgs)
 
            ' Notify the DataGridView that the contents of the cell have changed.
            valueIsChanged = True
            Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)
            MyBase.OnValueChanged(eventargs)
 
        End Sub
 
    End Class

Open in new window

Author

Commented:
Thanks !
My be you have code in c3

Author

Commented:
ops c#

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial