.NET 4.x DataGridViewNumericColumn не принимает значение в последней строке с 1 раза - Visual Basic .NET

Узнай цену своей работы

Формулировка задачи:

Гуру vb.net помогите разобраться. Нужна была удобная колонка для ввода суммы, нашел на просторах интернета этот вариант:
Листинг программы
  1. Public Class DataGridViewNumericColumn
  2. Inherits DataGridViewColumn
  3. Public Sub New()
  4. MyBase.New(New NumericUpDownCell())
  5. End Sub
  6. Public Overrides Property CellTemplate() As DataGridViewCell
  7. Get
  8. Return MyBase.CellTemplate
  9. End Get
  10. Set(ByVal value As DataGridViewCell)
  11. ' Ensure that the cell used for the template is a CalendarCell.
  12. If Not (value Is Nothing) AndAlso Not value.GetType().IsAssignableFrom(GetType(NumericUpDownCell)) Then
  13. Throw New InvalidCastException("Must be a NumericUpDownCell")
  14. End If
  15. MyBase.CellTemplate = value
  16. End Set
  17. End Property
  18. End Class
  19.  
  20. Public Class NumericUpDownCell
  21. Inherits DataGridViewTextBoxCell
  22. Public Sub New()
  23. ' Use the short date format.
  24. ' Me.Style.Format = "#.##"
  25. End Sub
  26. Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, ByVal initialFormattedValue As Object, ByVal dataGridViewCellStyle As DataGridViewCellStyle)
  27. ' Set the value of the editing control to the current cell value.
  28. MyBase.InitializeEditingControl(rowIndex, Me.Value, dataGridViewCellStyle) 'initialFormattedValue
  29. Dim ctl As NumericUpDownEditingControl = CType(DataGridView.EditingControl, NumericUpDownEditingControl)
  30. ctl.Value = 0.00
  31. Select Case dataGridViewCellStyle.Format
  32. Case "n0"
  33. ctl.DecimalPlaces = 0
  34. Case "n1"
  35. ctl.DecimalPlaces = 1
  36. Case "n2", "c2"
  37. ctl.DecimalPlaces = 2
  38. End Select
  39.  
  40. If Not Me.Value Is DBNull.Value Then
  41. If Not Me.Value Is Nothing Then
  42. ctl.Value = Me.Value
  43. End If
  44. End If
  45. End Sub
  46. Public Overrides ReadOnly Property EditType() As Type
  47. Get
  48. ' Return the type of the editing contol that CalendarCell uses.
  49. Return GetType(NumericUpDownEditingControl)
  50. End Get
  51. End Property
  52. Public Overrides ReadOnly Property ValueType() As Type
  53. Get
  54. ' Return the type of the value that CalendarCell contains.
  55. Return GetType(Decimal)
  56. End Get
  57. End Property
  58. Public Overrides ReadOnly Property DefaultNewRowValue() As Object
  59. Get
  60. ' Use the current date and time as the default value.
  61. Return Nothing
  62. End Get
  63. End Property
  64. End Class
  65.  
  66. Class NumericUpDownEditingControl
  67. Inherits NumericUpDown
  68. Implements IDataGridViewEditingControl
  69. Private dataGridViewControl As DataGridView
  70. Private valueIsChanged As Boolean = False
  71. Private rowIndexNum As Integer
  72. Public Sub New()
  73. Me.DecimalPlaces = 2
  74. Minimum = 0
  75. Maximum = 9999999999
  76. Value = 0.00
  77. End Sub
  78. Public Property EditingControlFormattedValue() As Object Implements IDataGridViewEditingControl.EditingControlFormattedValue
  79. Get
  80. Return Me.Value.ToString '("#.##")
  81. End Get
  82. Set(ByVal value As Object)
  83. If TypeOf value Is Decimal Then
  84. Me.Value = Decimal.Parse(value.ToString)
  85. End If
  86. End Set
  87. End Property
  88. Public Function GetEditingControlFormattedValue(ByVal context As DataGridViewDataErrorContexts) As Object Implements IDataGridViewEditingControl.GetEditingControlFormattedValue
  89. Return Me.Value.ToString '("#.##")
  90. End Function
  91. Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As DataGridViewCellStyle) Implements IDataGridViewEditingControl.ApplyCellStyleToEditingControl
  92. Me.Font = dataGridViewCellStyle.Font
  93. Me.ForeColor = dataGridViewCellStyle.ForeColor
  94. Me.BackColor = dataGridViewCellStyle.BackColor
  95. End Sub
  96. Public Property EditingControlRowIndex() As Integer Implements IDataGridViewEditingControl.EditingControlRowIndex
  97. Get
  98. Return rowIndexNum
  99. End Get
  100. Set(ByVal value As Integer)
  101. rowIndexNum = value
  102. End Set
  103. End Property
  104. Public Function EditingControlWantsInputKey(ByVal key As Keys, ByVal dataGridViewWantsInputKey As Boolean) As Boolean Implements IDataGridViewEditingControl.EditingControlWantsInputKey
  105. ' Let the DateTimePicker handle the keys listed.
  106. Select Case key And Keys.KeyCode
  107. Case Keys.Left, Keys.Up, Keys.Down, Keys.Right, Keys.Home, Keys.End, Keys.PageDown, Keys.PageUp
  108. Return True
  109. Case Else
  110. Return False
  111. End Select
  112. End Function
  113. Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) Implements IDataGridViewEditingControl.PrepareEditingControlForEdit
  114. ' No preparation needs to be done.
  115. End Sub
  116. Public ReadOnly Property RepositionEditingControlOnValueChange() As Boolean Implements IDataGridViewEditingControl.RepositionEditingControlOnValueChange
  117. Get
  118. Return False
  119. End Get
  120. End Property
  121. Public Property EditingControlDataGridView() As DataGridView Implements IDataGridViewEditingControl.EditingControlDataGridView
  122. Get
  123. Return dataGridViewControl
  124. End Get
  125. Set(ByVal value As DataGridView)
  126. dataGridViewControl = value
  127. End Set
  128. End Property
  129. Public Property EditingControlValueChanged() As Boolean Implements IDataGridViewEditingControl.EditingControlValueChanged
  130. Get
  131. Return valueIsChanged
  132. End Get
  133. Set(ByVal value As Boolean)
  134. valueIsChanged = True 'value
  135. End Set
  136. End Property
  137. Public ReadOnly Property EditingControlCursor() As Cursor Implements IDataGridViewEditingControl.EditingPanelCursor
  138. Get
  139. Return MyBase.Cursor
  140. End Get
  141. End Property
  142. Protected Overrides Sub OnValueChanged(ByVal eventargs As EventArgs)
  143. ' Notify the DataGridView that the contents of the cell have changed.
  144. valueIsChanged = True
  145. Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)
  146. MyBase.OnValueChanged(eventargs)
  147. End Sub
  148. End Class
Проблема такая, если в Datagridview строка не последняя, то введенное значение принимается и отображается в ячейке, если строка последняя значение вроде как принимается и строка новая создается (DatagridView c параметром AllowUserToAddRows=True) но не отображается, отображается только после повторного ввода. Как сделать чтобы в последней строке введенное значение сразу отображалось?

Решение задачи: «.NET 4.x DataGridViewNumericColumn не принимает значение в последней строке с 1 раза»

textual
Листинг программы
  1.   Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) Implements IDataGridViewEditingControl.PrepareEditingControlForEdit
  2.         ' No preparation needs to be done.
  3.         SendKeys.Send("{UP}") 'меняем значение на 1 вверх
  4.         SendKeys.Send("{DOWN}") ' вниз
  5.         SendKeys.Send("+{RIGHT}") ' выделяем первый 0
  6.     End Sub

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

8   голосов , оценка 4.25 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы