2015-11-24 20 views
0

我無法獲得我在網上找到的控件來接受句點(。)。我需要能夠輸入帶小數點的數字值。我想在datagridview單元格中使用numericupdown控件,所以我可以使用上下箭頭來調整值。自定義datagridview列控件將不會接受一個句點(。)

此控件實現NumericUpDown控件作爲datagridview列上的編輯控件。我發現它在線(不記得在哪裏),並且ti基於基於日曆控件的類似自定義datagridview列。

我對它做了一些修改,所以我可以設置最大值,最小值,小數點位數和遞增屬性。

但是,即使當小數位數設置爲2並且增量爲.1時,當我鍵入一個值時,控件將不會接受一個句點。

下面是代碼,其中包括列,單元格和編輯控件的類。請幫忙。我不知道問題是什麼。

 

    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 
     Private _Maximum As Decimal = 100 
     Private _Minimum As Decimal = 0 
     Private _Increment As Decimal = 0.1 
     Private _DecimalPlaces As Integer = 2 

     Public Property DecimalPlaces() As Integer 
      Get 
       Return _DecimalPlaces 
      End Get 
      Set(ByVal value As Integer) 
       If _DecimalPlaces = value Then 
        Return 
       End If 
       _DecimalPlaces = value 
      End Set 
     End Property 

     Public Property Maximum() As Decimal 
      Get 
       Return _Maximum 
      End Get 
      Set(ByVal value As Decimal) 
       _Maximum = value 
      End Set 
     End Property 
     _ 
     Public Property Minimum() As Decimal 
      Get 
       Return _Minimum 
      End Get 
      Set(ByVal value As Decimal) 
       _Minimum = value 
      End Set 

     End Property 
     _ 
     Public Property Increment() As Decimal 
      Get 
       Return _Increment 
      End Get 
      Set(ByVal value As Decimal) 
       _Increment = value 
      End Set 

     End Property 
    End Class 

 

    Public Class NumericUpDownCell 
     Inherits DataGridViewTextBoxCell 

     Public Sub New() 
      ' Use the short date format. 
      Me.Style.Format = "N2" 
     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) 
      RemoveHandler ctl.Enter, AddressOf Me.OnNumericEnter 
      AddHandler ctl.Enter, AddressOf Me.OnNumericEnter 
      ctl.Maximum = CType(Me.DataGridView.Columns(Me.ColumnIndex), NumericUpDownColumn).Maximum 
      ctl.Minimum = CType(Me.DataGridView.Columns(Me.ColumnIndex), NumericUpDownColumn).Minimum 
      ctl.Increment = CType(Me.DataGridView.Columns(Me.ColumnIndex), NumericUpDownColumn).Increment 
      ctl.DecimalPlaces = CType(Me.DataGridView.Columns(Me.ColumnIndex), NumericUpDownColumn).DecimalPlaces 
      ctl.ThousandsSeparator = True 
      ctl.Value = CType(Me.Value, Decimal) 

     End Sub 
     ''' 
     ''' Handle on enter event of numeric 
     ''' 
     ''' 
     ''' 
     ''' 
     Private Sub OnNumericEnter(ByVal sender As Object, ByVal e As EventArgs) 
      Dim control As NumericUpDownEditingControl = CType(sender, NumericUpDownEditingControl) 
      Dim strValue As String = control.Value.ToString("N2") 
      control.Select(0, strValue.Length) 
     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() 



     End Sub 

     Public Property EditingControlFormattedValue() As Object _ 
      Implements IDataGridViewEditingControl.EditingControlFormattedValue 

      Get 
       Return Me.Value.ToString("N2") 
      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("N2") 

     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 
       Case Keys.Up, Keys.Down 

        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) 

      ' Notify the DataGridView that the contents of the cell have changed. 
      valueIsChanged = True 
      Me.EditingControlDataGridView.NotifyCurrentCellDirty(True) 
      MyBase.OnValueChanged(eventargs) 

     End Sub 

    End Class 

+0

多數民衆贊成如何工作。 '即使小數點位置設置爲2,增量爲.1'*尤其*當允許小數點時,用戶可以在使用KB而不是箭頭時輸入小數點。對Value.ToString(「N2」)進行硬編碼可能是一個錯誤,它會忽略用戶在單元格樣式中設置的任何值,如果*爲非十進制,則在輸出中顯示一個值可能會令人困惑 – Plutonix

+0

這樣做2個地方。你指的是哪一個?事情是,當我輸入一段時間它不會顯示在編輯控件中。這就好像我沒有輸入任何東西。我會嘗試刪除這兩個'ToString(「N2」)'的發生。我輸入時仍未顯示。 – Marshall

+0

我無法重現 - 它讓我輸入小數。但它也不會保存對設置的更改。如果我更改小數或最小,最大它只使用您的默認值。你忘了序列化它們 – Plutonix

回答

0

我用一個老程序員招解決這個問題。我剛剛從文章Plutonix中下載了示例代碼,並將它附帶的DLL添加到了我的項目中。這項工作很好,爲我節省了很多麻煩,我沒有找到。

相關問題