locked
Decimal Values are not allow in DGV RRS feed

  • Question

  • HI,

    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 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
    

    Monday, September 24, 2018 11:44 AM

All replies

  • 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
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Proposed as answer by laurens vdb Monday, September 24, 2018 12:07 PM
    Monday, September 24, 2018 12:04 PM