Round cell values of a datatable (VB.NET)











up vote
0
down vote

favorite












I have trouble rounding some cells from my datatable.
I want this round to 2 decimal places, as you can imagine. I will explain quickly how I charge data to the DataTable :



I have this function stored in a class:



Protected Friend Function cargarPref(ByVal id_Pref As String) As DataTable
Dim cmd As String = "Select Material,Cubicaje,SubTotal,ITBM,Total from Preferencia WHERE Id_Preferencia=@id_Pref"
Dim t As New DataTable
Try
con.Open()
comando = New OleDbCommand(cmd, con)
comando.Parameters.AddWithValue("@id_Pref", id_Pref)
adapter = New OleDbDataAdapter(comando)
adapter.Fill(t)
comando.Dispose()
adapter.Dispose()
con.Close()
Catch ex As Exception
MsgBox("Error en la consulta: " + ex.Message, MsgBoxStyle.Critical)
End Try
Return t
End Function


Ok, now I call it in my windows form:



 Private Sub DataCliente_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataCliente.CellContentClick
If (Not IsNothing(DataMate)) Then
DataMate.DataSource = Nothing
mt.Clear()
End If
Dim index As Integer = 0
If (DataCliente.Columns(DataCliente.CurrentCell.ColumnIndex).Name.Equals("Empresa")) Then
index = Me.DataCliente.CurrentRow.Index
lblid.Text = DataCliente.Rows(index).Cells("Id_Cliente").Value
lblempresa.Text = DataCliente.Rows(index).Cells("Empresa").Value
lbldirecc.Text = DataCliente.Rows(index).Cells("Direccion").Value
lblcorreo.Text = DataCliente.Rows(index).Cells("Correo").Value
lbltel.Text = DataCliente.Rows(index).Cells("Telefono").Value
lblpreyd.Text = Math.Round(CDbl(DataCliente.Rows(index).Cells("PrecioYD").Value), 2).ToString("N2")
End If
mt = data.cargarPref(DataCliente.Rows(index).Cells("Id_Preferencia").Value) <--HERE!!!
DataMate.DataSource = mt
PanelMaterial.Enabled = True
End Sub


Now, watch as my values ​​are in my database access, Along With the datagrid of the program!



enter image description hereenter image description here
For some strange reason ... the data I have taken from the database , the program treats them as if they were integers.



How I can then round off those values ​​that are within the datatable, before printing in datagridview?










share|improve this question




























    up vote
    0
    down vote

    favorite












    I have trouble rounding some cells from my datatable.
    I want this round to 2 decimal places, as you can imagine. I will explain quickly how I charge data to the DataTable :



    I have this function stored in a class:



    Protected Friend Function cargarPref(ByVal id_Pref As String) As DataTable
    Dim cmd As String = "Select Material,Cubicaje,SubTotal,ITBM,Total from Preferencia WHERE Id_Preferencia=@id_Pref"
    Dim t As New DataTable
    Try
    con.Open()
    comando = New OleDbCommand(cmd, con)
    comando.Parameters.AddWithValue("@id_Pref", id_Pref)
    adapter = New OleDbDataAdapter(comando)
    adapter.Fill(t)
    comando.Dispose()
    adapter.Dispose()
    con.Close()
    Catch ex As Exception
    MsgBox("Error en la consulta: " + ex.Message, MsgBoxStyle.Critical)
    End Try
    Return t
    End Function


    Ok, now I call it in my windows form:



     Private Sub DataCliente_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataCliente.CellContentClick
    If (Not IsNothing(DataMate)) Then
    DataMate.DataSource = Nothing
    mt.Clear()
    End If
    Dim index As Integer = 0
    If (DataCliente.Columns(DataCliente.CurrentCell.ColumnIndex).Name.Equals("Empresa")) Then
    index = Me.DataCliente.CurrentRow.Index
    lblid.Text = DataCliente.Rows(index).Cells("Id_Cliente").Value
    lblempresa.Text = DataCliente.Rows(index).Cells("Empresa").Value
    lbldirecc.Text = DataCliente.Rows(index).Cells("Direccion").Value
    lblcorreo.Text = DataCliente.Rows(index).Cells("Correo").Value
    lbltel.Text = DataCliente.Rows(index).Cells("Telefono").Value
    lblpreyd.Text = Math.Round(CDbl(DataCliente.Rows(index).Cells("PrecioYD").Value), 2).ToString("N2")
    End If
    mt = data.cargarPref(DataCliente.Rows(index).Cells("Id_Preferencia").Value) <--HERE!!!
    DataMate.DataSource = mt
    PanelMaterial.Enabled = True
    End Sub


    Now, watch as my values ​​are in my database access, Along With the datagrid of the program!



    enter image description hereenter image description here
    For some strange reason ... the data I have taken from the database , the program treats them as if they were integers.



    How I can then round off those values ​​that are within the datatable, before printing in datagridview?










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have trouble rounding some cells from my datatable.
      I want this round to 2 decimal places, as you can imagine. I will explain quickly how I charge data to the DataTable :



      I have this function stored in a class:



      Protected Friend Function cargarPref(ByVal id_Pref As String) As DataTable
      Dim cmd As String = "Select Material,Cubicaje,SubTotal,ITBM,Total from Preferencia WHERE Id_Preferencia=@id_Pref"
      Dim t As New DataTable
      Try
      con.Open()
      comando = New OleDbCommand(cmd, con)
      comando.Parameters.AddWithValue("@id_Pref", id_Pref)
      adapter = New OleDbDataAdapter(comando)
      adapter.Fill(t)
      comando.Dispose()
      adapter.Dispose()
      con.Close()
      Catch ex As Exception
      MsgBox("Error en la consulta: " + ex.Message, MsgBoxStyle.Critical)
      End Try
      Return t
      End Function


      Ok, now I call it in my windows form:



       Private Sub DataCliente_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataCliente.CellContentClick
      If (Not IsNothing(DataMate)) Then
      DataMate.DataSource = Nothing
      mt.Clear()
      End If
      Dim index As Integer = 0
      If (DataCliente.Columns(DataCliente.CurrentCell.ColumnIndex).Name.Equals("Empresa")) Then
      index = Me.DataCliente.CurrentRow.Index
      lblid.Text = DataCliente.Rows(index).Cells("Id_Cliente").Value
      lblempresa.Text = DataCliente.Rows(index).Cells("Empresa").Value
      lbldirecc.Text = DataCliente.Rows(index).Cells("Direccion").Value
      lblcorreo.Text = DataCliente.Rows(index).Cells("Correo").Value
      lbltel.Text = DataCliente.Rows(index).Cells("Telefono").Value
      lblpreyd.Text = Math.Round(CDbl(DataCliente.Rows(index).Cells("PrecioYD").Value), 2).ToString("N2")
      End If
      mt = data.cargarPref(DataCliente.Rows(index).Cells("Id_Preferencia").Value) <--HERE!!!
      DataMate.DataSource = mt
      PanelMaterial.Enabled = True
      End Sub


      Now, watch as my values ​​are in my database access, Along With the datagrid of the program!



      enter image description hereenter image description here
      For some strange reason ... the data I have taken from the database , the program treats them as if they were integers.



      How I can then round off those values ​​that are within the datatable, before printing in datagridview?










      share|improve this question















      I have trouble rounding some cells from my datatable.
      I want this round to 2 decimal places, as you can imagine. I will explain quickly how I charge data to the DataTable :



      I have this function stored in a class:



      Protected Friend Function cargarPref(ByVal id_Pref As String) As DataTable
      Dim cmd As String = "Select Material,Cubicaje,SubTotal,ITBM,Total from Preferencia WHERE Id_Preferencia=@id_Pref"
      Dim t As New DataTable
      Try
      con.Open()
      comando = New OleDbCommand(cmd, con)
      comando.Parameters.AddWithValue("@id_Pref", id_Pref)
      adapter = New OleDbDataAdapter(comando)
      adapter.Fill(t)
      comando.Dispose()
      adapter.Dispose()
      con.Close()
      Catch ex As Exception
      MsgBox("Error en la consulta: " + ex.Message, MsgBoxStyle.Critical)
      End Try
      Return t
      End Function


      Ok, now I call it in my windows form:



       Private Sub DataCliente_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataCliente.CellContentClick
      If (Not IsNothing(DataMate)) Then
      DataMate.DataSource = Nothing
      mt.Clear()
      End If
      Dim index As Integer = 0
      If (DataCliente.Columns(DataCliente.CurrentCell.ColumnIndex).Name.Equals("Empresa")) Then
      index = Me.DataCliente.CurrentRow.Index
      lblid.Text = DataCliente.Rows(index).Cells("Id_Cliente").Value
      lblempresa.Text = DataCliente.Rows(index).Cells("Empresa").Value
      lbldirecc.Text = DataCliente.Rows(index).Cells("Direccion").Value
      lblcorreo.Text = DataCliente.Rows(index).Cells("Correo").Value
      lbltel.Text = DataCliente.Rows(index).Cells("Telefono").Value
      lblpreyd.Text = Math.Round(CDbl(DataCliente.Rows(index).Cells("PrecioYD").Value), 2).ToString("N2")
      End If
      mt = data.cargarPref(DataCliente.Rows(index).Cells("Id_Preferencia").Value) <--HERE!!!
      DataMate.DataSource = mt
      PanelMaterial.Enabled = True
      End Sub


      Now, watch as my values ​​are in my database access, Along With the datagrid of the program!



      enter image description hereenter image description here
      For some strange reason ... the data I have taken from the database , the program treats them as if they were integers.



      How I can then round off those values ​​that are within the datatable, before printing in datagridview?







      vb.net datatable






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 6:45









      Cœur

      17.1k9102140




      17.1k9102140










      asked May 17 '16 at 17:42









      TwoDent

      219317




      219317
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          I finally discovered a solution , and I am going to show you how to solve this problem, if you need it (Before you do this , you must define all the columns you want to see on your datagridview !):



          First, I create a class with public properties:



          Public Class Preferencia
          Public Property Material() As String
          Public Property Cubicaje() As String
          Public Property SubTotal() As String
          Public Property ITBM() As String
          Public Property Total() As String


          End Class



          Second , I create 2 functions and one procedural method :



           Private Function setPreferencia(ByVal material As String, ByVal cubicaje As String, ByVal subtotal As String, ByVal itbm As String, ByVal total As String) As Preferencia
          Dim item As New Preferencia
          item.Material = material
          item.Cubicaje = FormatNumber(cubicaje, 2)
          item.SubTotal = FormatNumber(subtotal, 2)
          item.ITBM = FormatNumber(itbm, 2)
          item.Total = FormatNumber(total, 2)
          Return item
          End Function

          Private Function registrarPreferencia() As List(Of Preferencia)
          Dim lista As New List(Of Preferencia)
          For Each itm In mt.Rows
          lista.Add(setPreferencia(itm(0), itm(1), itm(2), itm(3), itm(4)))
          Next
          Return lista

          Protected Friend Sub FillGrid()
          DataMate.AutoGenerateColumns = False
          DataMate.DataSource = registrarPreferencia()
          DataMate.Columns("Material").DataPropertyName = "Material"
          DataMate.Columns("Cubicaje").DataPropertyName = "Cubicaje"
          DataMate.Columns("SubTotal").DataPropertyName = "SubTotal"
          DataMate.Columns("ITBM").DataPropertyName = "ITBM"
          DataMate.Columns("Total").DataPropertyName = "Total"
          End Sub


          Now the only thing is to call the method " FillGrid ()" when you want to print the data. And finaly, I resolved it!



          enter image description here






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37283011%2fround-cell-values-of-a-datatable-vb-net%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            I finally discovered a solution , and I am going to show you how to solve this problem, if you need it (Before you do this , you must define all the columns you want to see on your datagridview !):



            First, I create a class with public properties:



            Public Class Preferencia
            Public Property Material() As String
            Public Property Cubicaje() As String
            Public Property SubTotal() As String
            Public Property ITBM() As String
            Public Property Total() As String


            End Class



            Second , I create 2 functions and one procedural method :



             Private Function setPreferencia(ByVal material As String, ByVal cubicaje As String, ByVal subtotal As String, ByVal itbm As String, ByVal total As String) As Preferencia
            Dim item As New Preferencia
            item.Material = material
            item.Cubicaje = FormatNumber(cubicaje, 2)
            item.SubTotal = FormatNumber(subtotal, 2)
            item.ITBM = FormatNumber(itbm, 2)
            item.Total = FormatNumber(total, 2)
            Return item
            End Function

            Private Function registrarPreferencia() As List(Of Preferencia)
            Dim lista As New List(Of Preferencia)
            For Each itm In mt.Rows
            lista.Add(setPreferencia(itm(0), itm(1), itm(2), itm(3), itm(4)))
            Next
            Return lista

            Protected Friend Sub FillGrid()
            DataMate.AutoGenerateColumns = False
            DataMate.DataSource = registrarPreferencia()
            DataMate.Columns("Material").DataPropertyName = "Material"
            DataMate.Columns("Cubicaje").DataPropertyName = "Cubicaje"
            DataMate.Columns("SubTotal").DataPropertyName = "SubTotal"
            DataMate.Columns("ITBM").DataPropertyName = "ITBM"
            DataMate.Columns("Total").DataPropertyName = "Total"
            End Sub


            Now the only thing is to call the method " FillGrid ()" when you want to print the data. And finaly, I resolved it!



            enter image description here






            share|improve this answer

























              up vote
              0
              down vote













              I finally discovered a solution , and I am going to show you how to solve this problem, if you need it (Before you do this , you must define all the columns you want to see on your datagridview !):



              First, I create a class with public properties:



              Public Class Preferencia
              Public Property Material() As String
              Public Property Cubicaje() As String
              Public Property SubTotal() As String
              Public Property ITBM() As String
              Public Property Total() As String


              End Class



              Second , I create 2 functions and one procedural method :



               Private Function setPreferencia(ByVal material As String, ByVal cubicaje As String, ByVal subtotal As String, ByVal itbm As String, ByVal total As String) As Preferencia
              Dim item As New Preferencia
              item.Material = material
              item.Cubicaje = FormatNumber(cubicaje, 2)
              item.SubTotal = FormatNumber(subtotal, 2)
              item.ITBM = FormatNumber(itbm, 2)
              item.Total = FormatNumber(total, 2)
              Return item
              End Function

              Private Function registrarPreferencia() As List(Of Preferencia)
              Dim lista As New List(Of Preferencia)
              For Each itm In mt.Rows
              lista.Add(setPreferencia(itm(0), itm(1), itm(2), itm(3), itm(4)))
              Next
              Return lista

              Protected Friend Sub FillGrid()
              DataMate.AutoGenerateColumns = False
              DataMate.DataSource = registrarPreferencia()
              DataMate.Columns("Material").DataPropertyName = "Material"
              DataMate.Columns("Cubicaje").DataPropertyName = "Cubicaje"
              DataMate.Columns("SubTotal").DataPropertyName = "SubTotal"
              DataMate.Columns("ITBM").DataPropertyName = "ITBM"
              DataMate.Columns("Total").DataPropertyName = "Total"
              End Sub


              Now the only thing is to call the method " FillGrid ()" when you want to print the data. And finaly, I resolved it!



              enter image description here






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                I finally discovered a solution , and I am going to show you how to solve this problem, if you need it (Before you do this , you must define all the columns you want to see on your datagridview !):



                First, I create a class with public properties:



                Public Class Preferencia
                Public Property Material() As String
                Public Property Cubicaje() As String
                Public Property SubTotal() As String
                Public Property ITBM() As String
                Public Property Total() As String


                End Class



                Second , I create 2 functions and one procedural method :



                 Private Function setPreferencia(ByVal material As String, ByVal cubicaje As String, ByVal subtotal As String, ByVal itbm As String, ByVal total As String) As Preferencia
                Dim item As New Preferencia
                item.Material = material
                item.Cubicaje = FormatNumber(cubicaje, 2)
                item.SubTotal = FormatNumber(subtotal, 2)
                item.ITBM = FormatNumber(itbm, 2)
                item.Total = FormatNumber(total, 2)
                Return item
                End Function

                Private Function registrarPreferencia() As List(Of Preferencia)
                Dim lista As New List(Of Preferencia)
                For Each itm In mt.Rows
                lista.Add(setPreferencia(itm(0), itm(1), itm(2), itm(3), itm(4)))
                Next
                Return lista

                Protected Friend Sub FillGrid()
                DataMate.AutoGenerateColumns = False
                DataMate.DataSource = registrarPreferencia()
                DataMate.Columns("Material").DataPropertyName = "Material"
                DataMate.Columns("Cubicaje").DataPropertyName = "Cubicaje"
                DataMate.Columns("SubTotal").DataPropertyName = "SubTotal"
                DataMate.Columns("ITBM").DataPropertyName = "ITBM"
                DataMate.Columns("Total").DataPropertyName = "Total"
                End Sub


                Now the only thing is to call the method " FillGrid ()" when you want to print the data. And finaly, I resolved it!



                enter image description here






                share|improve this answer












                I finally discovered a solution , and I am going to show you how to solve this problem, if you need it (Before you do this , you must define all the columns you want to see on your datagridview !):



                First, I create a class with public properties:



                Public Class Preferencia
                Public Property Material() As String
                Public Property Cubicaje() As String
                Public Property SubTotal() As String
                Public Property ITBM() As String
                Public Property Total() As String


                End Class



                Second , I create 2 functions and one procedural method :



                 Private Function setPreferencia(ByVal material As String, ByVal cubicaje As String, ByVal subtotal As String, ByVal itbm As String, ByVal total As String) As Preferencia
                Dim item As New Preferencia
                item.Material = material
                item.Cubicaje = FormatNumber(cubicaje, 2)
                item.SubTotal = FormatNumber(subtotal, 2)
                item.ITBM = FormatNumber(itbm, 2)
                item.Total = FormatNumber(total, 2)
                Return item
                End Function

                Private Function registrarPreferencia() As List(Of Preferencia)
                Dim lista As New List(Of Preferencia)
                For Each itm In mt.Rows
                lista.Add(setPreferencia(itm(0), itm(1), itm(2), itm(3), itm(4)))
                Next
                Return lista

                Protected Friend Sub FillGrid()
                DataMate.AutoGenerateColumns = False
                DataMate.DataSource = registrarPreferencia()
                DataMate.Columns("Material").DataPropertyName = "Material"
                DataMate.Columns("Cubicaje").DataPropertyName = "Cubicaje"
                DataMate.Columns("SubTotal").DataPropertyName = "SubTotal"
                DataMate.Columns("ITBM").DataPropertyName = "ITBM"
                DataMate.Columns("Total").DataPropertyName = "Total"
                End Sub


                Now the only thing is to call the method " FillGrid ()" when you want to print the data. And finaly, I resolved it!



                enter image description here







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered May 17 '16 at 21:56









                TwoDent

                219317




                219317






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37283011%2fround-cell-values-of-a-datatable-vb-net%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Florida Star v. B. J. F.

                    Danny Elfman

                    Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues