DGV中不允许使用十进制值 [英] Decimal Values are not allow in DGV
本文介绍了DGV中不允许使用十进制值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
HI,
我只在某些DGV单元格中为数值设置了一些功能键,但是它只允许我使用0到9个字符写小数值。这里是代码
I set some function key for numerical value only in some of my DGV cells but it allows me to only 0-9 characters it does not allow me to write decimal values. here are code
plz提供纠正这些的建议
plz provide suggestion to correct these
Public Class clsfunction
Public con As New SqlClient.SqlConnection(My.Settings.AgrasenConnectionString)
Public cmd As New SqlClient.SqlCommand
Dim Numstr As String = "0123456789.-/"
Public Sub keyPressNumeric(ByVal pflag As Boolean, ByVal e As KeyPressEventArgs)
If pflag = True Then
If Not IsNumeric(e.KeyChar.ToString) And Not e.KeyChar = Chr(Keys.Back) Then
e.Handled = True
End If
ElseIf pflag = False Then
If IsNumeric(e.KeyChar.ToString) Then e.Handled = True
End If
End Sub
Public Sub KeyPressNumeric(ByVal e As KeyPressEventArgs)
If Not IsNumeric(e.KeyChar.ToString) And Not e.KeyChar = Chr(Keys.Back) Then
e.Handled = True
End If
End Sub
Public Sub KeyPressNonNumeric(ByVal e As KeyPressEventArgs)
If IsNumeric(e.KeyChar.ToString) Then e.Handled = True
End Sub
-----------------------------------------------------------------------
Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
If Me.DataGridView1.CurrentCell.ColumnIndex = 6 Or Me.DataGridView1.CurrentCell.ColumnIndex = 7 Or Me.DataGridView1.CurrentCell.ColumnIndex = 9 Or Me.DataGridView1.CurrentCell.ColumnIndex = 11 Then
Dim txtNum As TextBox = e.Control
RemoveHandler txtNum.KeyPress, AddressOf keypress_gridviewcell
AddHandler txtNum.KeyPress, AddressOf keypress_gridviewcell
Else
'Dim ctrl As Control = e.Control
'If (ctrl.GetType) Is GetType(TextBox) Then
Dim txtNum As TextBox = e.Control
RemoveHandler txtNum.KeyPress, AddressOf keypress_gridviewcell
End If
'End If
End Sub
推荐答案
尝试这样的事情。
Try something like this.
Public Class Form1
Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
If Not DataGridView1.Rows(e.RowIndex).IsNewRow Then
If DataGridView1.Columns(e.ColumnIndex).Name.Equals("RatingColumn") Then
If Not e.Value.ToString.Contains(",") AndAlso Not e.Value.ToString.Contains(".") Then
e.Value = e.Value.ToString & ",00"
End If
If e.Value.ToString.Contains(".") Then
e.Value = e.Value.ToString.Replace(".", ",")
End If
End If
End If
End Sub
Sub DataGridView1_CellValidating(ByVal sender As Object, ByVal e As DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
Dim cell As DataGridViewCell = DataGridView1.Item(e.ColumnIndex, e.RowIndex)
If cell.IsInEditMode Then
Dim c As Control = DataGridView1.EditingControl
If DataGridView1.Columns(e.ColumnIndex).Name = "RatingColumn" Then
Dim DecValue As Decimal = 0
If Not Decimal.TryParse(c.Text, DecValue) Then
MessageBox.Show("Not valid")
e.Cancel = True
End If
End If
End If
End Sub
End Class
这篇关于DGV中不允许使用十进制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文