VB.NET datagridview EndEdit decimals with "." (period)

1

When you change the cell of a DataGridView and say "230.50" for example, it becomes "23050.00".

I was looking for a way to take the value in the event CellEndEdit and do a Replace(".",",") but there is no way. Once you press enter the variable that saves the value of the cell deletes the% "." separator and converts everything to integer ( 23050 ).

the column in question stores a price that is charged to the sellers by hand when they reach a certain sales target. The AWARD column is of DOUBLE type with 2 decimals, and the database is in Access.

Any suggestions?

    
asked by Gonzalo 02.08.2016 в 01:05
source

2 answers

2

If the column in question is intended to store currency values, it is better to use this type of data ( Currency or Currency or Decimal ) instead of Double .

The error is not due to that detail, which is only intended as a tip for better programming.

The problem is because the regional settings are different between the operating system and the user.

In this case, the operating system expects the user to use the comma as a decimal separator. But the user is placing the point.

What we can do, in addition to properly instructing users, is to order our software to correct the data they enter.

We can build on an example of the msdn to make the following solution:

Private Sub DataGridView1_CellParsing(sender As Object, e As System.Windows.Forms.DataGridViewCellParsingEventArgs) Handles DataGridView1.CellParsing

    If e IsNot Nothing Then
        If Me.DataGridView1.Columns(e.ColumnIndex).Name.ToUpper = "PREMIO" Then                
            If e.Value IsNot Nothing Then
                Try
                    ' Obtiene el valor que introdujo el usuario '
                    Dim valor$ = e.Value.ToString

                    ' Reemplaza el punto por la coma, y convierte a Double '
                    ' Es muy importante aquí la conversión explícita, porque la implícita no funcionará '
                    e.Value = CDbl(valor.Replace(".", ","))

                    ' Indica que el evento ha sido manipulado por el programador '
                    e.ParsingApplied = True

                Catch ex As FormatException
                    ' En caso de error, no pudo haber corrección del valor '
                    e.ParsingApplied = False
                End Try
            End If
        End If
    End If
End Sub
    
answered by 09.08.2016 / 03:03
source
0

My suggestion may work for you, it worked for me on a large project in the right way. As I had problems with the entry methods and the "formats" of decimals, what I decided was to modify this configuration of each terminal in the program itself with the following routine:

 Public Sub CambiarRegistro()
    Dim regVersion = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(
              "Control Panel\International", True)
    If regVersion Is Nothing Then
        ' Key doesn't exist; create it.
        MsgBox("NoExiste")
    End If

    Dim clave As String
    If regVersion IsNot Nothing Then
        clave = regVersion.GetValue("sDecimal")
        'MsgBox(clave)
        regVersion.SetValue("sDecimal", ".")
        regVersion.SetValue("sList", ",")
        regVersion.SetValue("sThousand", ",")
        regVersion.Close()
    End If
    'Dim reg64 As String = (Application.StartupPath() & "\data\config.reg")
    'Dim regedit As String = "regedit.exe"

    '' New ProcessStartInfo created
    'Dim p As New ProcessStartInfo
    '' Specify the location of regedit
    'p.FileName = regedit
    'p.Arguments = "/s  """ & reg64
    'Process.Start(p)
End Sub

With this routine (which I run at the beginning of the application) I make sure that my decimal separator is the "." and not the ",".

Try it and tell me.

    
answered by 02.08.2016 в 16:57