有一个自定义的 NumericUpDownColumn:
Module NumUpDownModule
Public defRowValue As String = "0"
End Module
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 NumericUpDownCell")
End If
MyBase.CellTemplate = value
End Set
End Property
End Class
Public Class NumericUpDownCell
Inherits DataGridViewTextBoxCell
Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, ByVal initialFormattedValue As Object, ByVal dataGridViewCellStyle As DataGridViewCellStyle)
MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)
Dim ctl As NumericUpDownEditingControl = CType(DataGridView.EditingControl, NumericUpDownEditingControl)
If Me.Value Is Nothing Or DBNull.Value.Equals(Me.Value) Then
ctl.Value = defRowValue
Else
ctl.Value = CType(Me.Value, Decimal)
End If
End Sub
Public Overrides ReadOnly Property EditType() As Type
Get
Return GetType(NumericUpDownEditingControl)
End Get
End Property
Public Overrides ReadOnly Property ValueType() As Type
Get
Return GetType(Decimal)
End Get
End Property
Public Overrides ReadOnly Property DefaultNewRowValue() As Object
Get
Return defRowValue
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 = 0
End Sub
Public Property EditingControlFormattedValue() As Object Implements IDataGridViewEditingControl.EditingControlFormattedValue
Get
Return Me.Value.ToString()
End Get
Set(ByVal value As Object)
Me.Value = Decimal.Parse(value.ToString())
End Set
End Property
Public Function GetEditingControlFormattedValue(ByVal context As DataGridViewDataErrorContexts) As Object Implements IDataGridViewEditingControl.GetEditingControlFormattedValue
If DBNull.Value.Equals(Me.Value) Then
Return defRowValue
Else
Return Me.Value.ToString()
End If
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 Cursor Implements IDataGridViewEditingControl.EditingPanelCursor
Get
Return MyBase.Cursor
End Get
End Property
Protected Overrides Sub OnValueChanged(ByVal eventargs As EventArgs)
valueIsChanged = True
Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)
MyBase.OnValueChanged(eventargs)
End Sub
End Class
DataGridView AllowUserToAddRows 属性设置为 FALSE。
添加新行时,NumericUpDownCell 中的默认值不显示。
添加新行时如何显示默认值?
如果AllowUserToAddRows属性更改为TRUE,则所有内容都会正确显示。然而它一定总是假的。
这取决于您如何填充网格。如果您将其绑定到
DataTable
或 BindingSource
又绑定到 DataTable
,那么您需要设置 DataColumn.DefaultValue
属性。
例如:
Dim dt = New DataTable()
dt.Columns.AddRange({
New DataColumn("TextColumn", GetType(String)),
New DataColumn("DecimalColumn", GetType(Decimal)) With {.DefaultValue = 0D}
})
DataGridView1.AutoGenerateColumns = False
DataGridView1.DataSource = New BindingSource(dt, Nothing)
DataGridView1.Columns.AddRange(
{
New DataGridViewTextBoxColumn With {
.Name = "TextColumn",
.DataPropertyName = .Name,
.HeaderText = "Some Text"
},
New NumericUpDownColumn With {
.Name = "DecimalColumn",
.DataPropertyName = .Name,
.HeaderText = "Value",
.SortMode = DataGridViewColumnSortMode.Automatic
}})
另一方面,如果您有未绑定的网格并使用
Rows
集合,则需要重写 DataGridViewCell.GetValue
方法以返回默认 Decimal
值而不是默认 Nothing
值。如果装箱类型为 Value
,则返回 Decimal
属性,或者将 DataGridViewCell.DefaultNewRowValue
属性的值转换为十进制。
Public Class NumericUpDownCell
Inherits DataGridViewTextBoxCell
Public Overrides Sub InitializeEditingControl(
rowIndex As Integer,
initialFormattedValue As Object,
dataGridViewCellStyle As DataGridViewCellStyle)
MyBase.InitializeEditingControl(
rowIndex,
initialFormattedValue,
dataGridViewCellStyle)
Dim ctl As NumericUpDownEditingControl = DirectCast(
DataGridView.EditingControl,
NumericUpDownEditingControl)
ctl.Value = GetValueOrDefault(Value)
End Sub
Public Overrides ReadOnly Property EditType() As Type
Get
Return GetType(NumericUpDownEditingControl)
End Get
End Property
Public Overrides ReadOnly Property ValueType() As Type
Get
Return GetType(Decimal)
End Get
End Property
Public Overrides ReadOnly Property DefaultNewRowValue() As Object
Get
Return 0D
End Get
End Property
Protected Overrides Function GetValue(rowIndex As Integer) As Object
Return GetValueOrDefault(MyBase.GetValue(rowIndex))
End Function
Private Function GetValueOrDefault(val As Object) As Decimal
Dim outVal As Decimal
If val IsNot Nothing AndAlso
val IsNot DBNull.Value AndAlso
Decimal.TryParse(val.ToString(), outVal) Then
Return outVal
Else
Return Convert.ToDecimal(DefaultNewRowValue)
End If
End Function
End Class