none
problem when comparing the stock in a grid vb2015 RRS feed

  • Question

  • good afternoon

    friends of the forum I am in a vbnet 2015 sql order form 2014 when trying to compare my stock with the accumulated amount of money I repeat the data

    when pressing the button increase I repeat the data of the grid
    should increase the amount and if it exceeds the stock I have I should show a message

    this the code

    ProductDetails = show.SearchProduct(ProductID)  search for the product id and load them on the grid //this right

    Private Sub Btnincrease_Click(sender As Object, e As EventArgs) Handles BtnAumentar.Click
    Dim Product As New CpE_Cls_PRODUCTO
    Dim objProduct As New CpN_Cls_PRODUCTO
    Dim stock As Integer
    
    Dim RowIndex As Integer = ProductsGridView.CurrentRow.Index
    Dim ProductID As Integer = Convert.ToInt32(ProductsGridView.CurrentRow.Cells("IdProducto").Value)
    
    
    If ProductsGridView.RowCount > 0 Then
    ProductDetails = show.SearchProduct(ProductID)  search for the product id and load them on the grid //this right
    'Producto.stock = Convert.ToInt32(ProductsGridView.CurrentRow.Cells(3).Value) Producto.stock = ProductDetails.stock stock = Producto.stock If CheckProductAlreadyAdded(ProductID) Then Dim Quantity As Integer = Convert.ToInt32(ProductsGridView.Rows(RowIndex).Cells("Quantity").Value) Dim Price As Decimal = Convert.ToInt32(ProductsGridView.Rows(RowIndex).Cells("Price").Value) Quantity += 1 '/Do this ... Important ... Have a decimal part in the total price Dim TotalPrice As Double = Convert.ToDouble(Quantity * Price) ProductsGridView.Rows(RowIndex).Cells("Quantity").Value = Quantity ProductsGridView.Rows(RowIndex).Cells("Price").Value = TotalPrice TotalBillBox.Text = CalculateTotalBill(ProductsGridView).ToString() If Quantity >= stock Then MessageBox.Show("The stock has been exceeded", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Warning) Exit Sub Else ProductsGridView.Rows.Add(ProductID, ProductDetails.Name, ProductDetails.price, 1, ProductDetails.price * 1) TotalBillBox.Text = CalculateTotalBill(ProductsGridView).ToString() End If End If End If End Sub

    it does not work

    If Quantity >= stock Then MessageBox.Show("The stock has been exceeded", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Warning) Exit Sub

    if you can help me please that the producer can not be sold because there is not enough stock

    Friday, March 9, 2018 5:48 PM

Answers

  • thanks for answering to Marry but where I should put the code that offers me I do not understand I'm a novice

    Change the CheckProductAlreadyAdded function so that it returns the row number of the item if it is found, or -1 if the row was not found. 

     Public Function CheckProductAlreadyAdded(ProductID As Integer) As Integer
    
            Dim RowIndex As Integer = -1
            For Each Row As DataGridViewRow In ProductsGridView.Rows
                If Convert.ToInt32(Row.Cells("IdProducto").Value) = ProductID Then
                    Return Row.Index
                End If
            Next
            Return -1
        End Function

    Change the main routine so that it uses the return value (a) to check whether or not the row was found and (b) as the row number if the product was found,

    Dim FoundRowIndex as Integer = CheckProductAlreadyAdded(ProductID) If FoundRowIndex > -1 Then Dim Quantity As Integer = Convert.ToInt32(ProductsGridView.Rows(FoundRowIndex).Cells("Cantidad").Value) Dim Price As Decimal = Convert.ToInt32(ProductsGridView.Rows(FoundRowIndex).Cells("Precio").Value) Quantity += 1 '/////////// <Hacer esto ... Importante ... Tener una parte decimal en el precio total> Dim TotalPrice As Double = Convert.ToDouble(Quantity * Price) ProductsGridView.Rows(FoundRowIndex).Cells("Cantidad").Value = Quantity ProductsGridView.Rows(FoundRowIndex).Cells("Importe").Value = TotalPrice TotalBillBox.Text = CalculateTotalBill(ProductsGridView).ToString() If stock >= Quantity Then MessageBox.Show("Se ha superado el stock", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Warning) Exit Sub
    End If Else ' Row was not found. Add it. ProductsGridView.Rows.Add(ProductID, ProductDetails.Nombre, ProductDetails.precio, 1, ProductDetails.precio * 1) TotalBillBox.Text = CalculateTotalBill(ProductsGridView).ToString()

            End If


    • Marked as answer by djnilo Thursday, March 15, 2018 6:00 PM
    Tuesday, March 13, 2018 9:57 PM
  • thanks for answering to Marry but where I should put the code that offers me I do not understand I'm a novice

    Change the CheckProductAlreadyAdded function so that it returns the row number of the item if it is found, or -1 if the row was not found. 

     Public Function CheckProductAlreadyAdded(ProductID As Integer) As Integer
    
            Dim RowIndex As Integer = -1
            For Each Row As DataGridViewRow In ProductsGridView.Rows
                If Convert.ToInt32(Row.Cells("IdProducto").Value) = ProductID Then
                    Return Row.Index
                End If
            Next
            Return -1
        End Function

    Change the main routine so that it uses the return value (a) to check whether or not the row was found and (b) as the row number if the product was found,

    Dim FoundRowIndex as Integer = CheckProductAlreadyAdded(ProductID) If FoundRowIndex > -1 Then Dim Quantity As Integer = Convert.ToInt32(ProductsGridView.Rows(FoundRowIndex).Cells("Cantidad").Value) Dim Price As Decimal = Convert.ToInt32(ProductsGridView.Rows(FoundRowIndex).Cells("Precio").Value) Quantity += 1 '/////////// <Hacer esto ... Importante ... Tener una parte decimal en el precio total> Dim TotalPrice As Double = Convert.ToDouble(Quantity * Price) ProductsGridView.Rows(FoundRowIndex).Cells("Cantidad").Value = Quantity ProductsGridView.Rows(FoundRowIndex).Cells("Importe").Value = TotalPrice TotalBillBox.Text = CalculateTotalBill(ProductsGridView).ToString() If stock >= Quantity Then MessageBox.Show("Se ha superado el stock", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Warning) Exit Sub
    End If Else ' Row was not found. Add it. ProductsGridView.Rows.Add(ProductID, ProductDetails.Nombre, ProductDetails.precio, 1, ProductDetails.precio * 1) TotalBillBox.Text = CalculateTotalBill(ProductsGridView).ToString()

            End If


    Acamar thank you very much for the patience and time that you had with me now works good

    • Marked as answer by djnilo Thursday, March 15, 2018 5:55 PM
    Thursday, March 15, 2018 5:55 PM

All replies

  • buenas tardes

    amigos del foro estoy en un formulario de pedidos vbnet 2015 sql 2014 al tratar de comparar mi stock con la cantidad ingresada acumulada se me repiten los datos  

    este el codigo 

     Private Sub BtnAumentar_Click(sender As Object, e As EventArgs) Handles BtnAumentar.Click
            Dim Producto As New CpE_Cls_PRODUCTO
            Dim objProducto As New CpN_Cls_PRODUCTO
            Dim stock As Integer
    
            Dim RowIndex As Integer = ProductsGridView.CurrentRow.Index
            Dim ProductID As Integer = Convert.ToInt32(ProductsGridView.CurrentRow.Cells("IdProducto").Value)
    
    
            If ProductsGridView.RowCount > 0 Then
                ProductDetails = mostrar.BuscarProducto(ProductID)
    
                Producto.stock = Convert.ToInt32(ProductsGridView.CurrentRow.Cells(3).Value)
                stock = Producto.stock
    
                'If CheckProductAlreadyAdded(ProductID) Then
    
                Dim Quantity As Integer = Convert.ToInt32(ProductsGridView.Rows(RowIndex).Cells("Cantidad").Value)
                Dim Price As Decimal = Convert.ToInt32(ProductsGridView.Rows(RowIndex).Cells("Precio").Value)
                Quantity += 1
                '/////////// <Hacer esto ... Importante ... Tener una parte decimal en el precio total>
                Dim TotalPrice As Double = Convert.ToDouble(Quantity * Price)
                ProductsGridView.Rows(RowIndex).Cells("Cantidad").Value = Quantity
    
                ProductsGridView.Rows(RowIndex).Cells("Importe").Value = TotalPrice
                TotalBillBox.Text = CalculateTotalBill(ProductsGridView).ToString()
    
                If stock >= Quantity Then
    
                    MessageBox.Show("Se ha superado el stock", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                    Exit Sub
    
                Else
                    ProductsGridView.Rows.Add(ProductID, ProductDetails.Nombre, ProductDetails.precio, 1, ProductDetails.precio * 1)
                    TotalBillBox.Text = CalculateTotalBill(ProductsGridView).ToString()
    
                End If
            End If
    
    
    
    
        End Sub

    problema esta aca 

    If stock >= Quantity Then
    
                    MessageBox.Show("Se ha superado el stock", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                    Exit Sub

    al trata de comparar el stock con la cantidad aumentar se me repiten los datos

    esta mal deberia ir aumentado la catidad hay mismo no bajar a la siguiente celda


    • Edited by djnilo Thursday, March 8, 2018 5:25 PM
    • Merged by Enrique M. Montejo Saturday, March 10, 2018 8:13 AM Duplicated question.
    Thursday, March 8, 2018 3:23 PM
  • Hello,

    When you say “sql order”, if this indicates the data is coming from a SQL-Server database then you should be loading this data either into a DataTable or a List(Of T). Either data source can then be set to the data source of a BindingSource component. The BindingSource becomes the data source of the DataGridView.

    Now when there is a need to interact with the data, if a DataTable you can subscribe to events of the DataTable to “learn” of changes in the data. If working with a List(Of T) then the list would be created using a connection and command coupled with a data reader to populate the list and in turn the list becomes the data source of a BindingSource component followed by setting the BindingSource to the DataGridView. Events (or delegates) can be subscribed too similarly to working with a DataTable.

    Using one of the above provides easy access to data from events or delegates. Note that the Current property of the BindingSource component provides access to the current row in the DataGridView so we can then access data without touching the DataGridView e.g.

    CType(bsProduct.Current,DataRowView).Row.Field(Of Integer)(“ IdProducto”), other fields the same way. Values can be set also CType(bsProduct.Current,DataRowView).Row.SetField(Of String)(“SomeFieldName”,”some value”)

    By accessing the data rather than cells in a DataGridView you are much better off and calculations are much simpler.

    If using a DataTable in this case you can also setup expressions for a DataColumn to do calculations without any intervention from you to write code.

    I don't have an example to match up to your specific requirements so if interested I suggest investigating what I have wrote, put sometime into it and you will be rewarded. Get stuck, ask questions on something, we are here to help but not provide working samples.


    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

    Friday, March 9, 2018 8:21 PM
    Moderator
  • when pressing the button increase I repeat the data of the grid

    should increase the amount and if it exceeds the stock I have I should show a message

    That suggests that the test ' If CheckProductAlreadyAdded(ProductID) Then ' is returning false. That is, the product ID is not found. You need to insert a breakpoint in your code at this point and confirm that the product ID is found, and if it isn't then you need to investigate the value of the ID to find out why.

    Friday, March 9, 2018 9:35 PM
  • thanks for answering Acamar
    Check the If CheckProductAlreadyAdded (ProductID) if it returns the value of the ProductId

     Public Function CheckProductAlreadyAdded (ProductID As Integer) As Boolean
        
             Dim RowIndex As Integer = Nothing
             For Each Row As DataGridViewRow In ProductsGridView.Rows
                 If Convert.ToInt32 (Row.Cells ("ProductID"). Value) = ProductID Then
                     RowIndex = Row.Index ' this is wrong I do not always say zero
                     Return True
                 End If
             Next
             Return False
         End Function

    but I have a doubt with this line always says zero

      RowIndex = Row.Index



    if I take this line, the code works well, this line makes the products repeat

       If Quantity> = stock Then
                         MessageBox.Show ("The stock has been exceeded", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                         Exit Sub


    Saturday, March 10, 2018 2:02 PM
  • thanks for answering kareninstructor
    so I bring the data by the id to load the products to grid then as it should bring

    business layer

    Function SearchProduct (ProductId As Integer)
             Dim objdatos As New CAPA_DATOS.CpD_Cls_PRODUCTO
             Return objdatos.RetreiveProductDetails (ProductId)
         End Function

    data layer

    Public Function RetreiveProductDetails (ProductID As Integer) As CpE_Cls_PRODUCT
            Dim Conn As New SqlConnection (Connection.Connectionstring)
            Dim Cmd As New SqlCommand
            Dim Result As CpE_Cls_PRODUCTO = Nothing
            Try
                Conn.Open ()
                Cmd.CommandText = "SELECT ProductID, Name, Price, Stock From Product Where ProductID = @ ProductID"
                Cmd.CommandType = CommandType.Text
                Cmd.Connection = Conn
                Cmd.Parameters.Add ("@ ProductID", SqlDbType.Int) .Value = ProductID
                Dim reader As SqlDataReader = Cmd.ExecuteReader
                Dim Products As New List (Of CpE_Cls_PRODUCTO)
                If reader.HasRows Then
                    Result = New CpE_Cls_PRODUCT
                    If reader.Read Then
                        Result.idproduct = reader.GetInt32 (0)
                        Result.Name = reader.GetString (1)
                        Result.price = reader.GetDecimal (2)
                        Result.stock = reader.GetInt32 (3)
                      
    
                    End If
                End If
                reader.Close ()
            Catch ex As Exception
                Throw New Exception (ex.Message)
            End Try
            Return Result
        End Function


    • Edited by djnilo Saturday, March 10, 2018 2:47 PM
    Saturday, March 10, 2018 2:08 PM
  • it does not work

    If Quantity >= stock Then MessageBox.Show("The stock has been exceeded", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Warning) Exit Sub

    if you can help me please that the producer can not be sold because there is not enough stock

    Hi djnilo,

    You said that it doesn't work, what it means?

    >>when pressing the button increase I repeat the data of the grid
    should increase the amount and if it exceeds the stock I have I should show a message

    What does the above sentence means? Can you describe more detailed info or give one example about what you want to do.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 12, 2018 2:32 AM
    Moderator
  • Check the If CheckProductAlreadyAdded (ProductID) if it returns the value of the ProductId

    You should explain what you expect to happen with that code.   RowIndex is a variable defined within the routine 'CheckProductAlreadyAdded'.  It can be used in that routine, but it will not be accessible to any code outside that rouine.   Do you want the row index to be available to other code?  If so then you should:

      Change the return type of the function from Boolean to Integer
      Set the return value (eg, RowIndex) to -1
      Set the return value (eg, RowIndex) to the row index IF there is a match with the ID
      Return that value (eg, RowIndex) as the result of the function

    Then, in the code that calls that function, change it so that a return value of :

     -1 means that there was no match.  The row needs to be added
     > -1 means that the return value is the row index of the item with the matching ID.

    I am assuming that the value of RowIndex is what you saw when you examined it in the calling routine.  You need to examine it in the 'CheckProductAlreadyAdded' routine.

     
    Monday, March 12, 2018 3:06 AM
  • thank you for answering Acamar try to do it but the same thing happens

    does not take nigun value RowIndex says zero



    • Edited by djnilo Monday, March 12, 2018 5:06 PM
    Monday, March 12, 2018 3:08 PM
  • does not take nigun value RowIndex says zero

    You need to get the return value of the function before you test it so you can use it for the row index IF the product was found. A return value of -1 means that there was no match and the row needs to be added, while a return value of 0 or greater means that there was a match and the return value is the row index of the item.

    Dim FoundRowIndex as Integer = CheckProductAlreadyAdded(ProductId)
    If FoundRowIndex > -1 then
      Dim Quantity as Integer = Convert.ToInt32 (ProdutsGridView.Rows(FoundRowIndex),cells(...
    ...
    Else ' If FoundRowIndex = -1
      ProductsGridView.Rows.Add(ProductID, ...


    Monday, March 12, 2018 9:09 PM
  • does not take nigun value RowIndex says zero

    You need to get the return value of the function before you test it so you can use it for the row index IF the product was found. A return value of -1 means that there was no match and the row needs to be added, while a return value of 0 or greater means that there was a match and the return value is the row index of the item.

    Dim FoundRowIndex as Integer = CheckProductAlreadyAdded(ProductId)
    If FoundRowIndex > -1 then
      Dim Quantity as Integer = Convert.ToInt32 (ProdutsGridView.Rows(FoundRowIndex),cells(...
    ...
    Else ' If FoundRowIndex = -1
      ProductsGridView.Rows.Add(ProductID, ...


    thanks for answering to Marry but where I should put the code that offers me I do not understand I'm a novice
    Tuesday, March 13, 2018 12:15 PM
  • does not take nigun value RowIndex says zero

    You need to get the return value of the function before you test it so you can use it for the row index IF the product was found. A return value of -1 means that there was no match and the row needs to be added, while a return value of 0 or greater means that there was a match and the return value is the row index of the item.

    Dim FoundRowIndex as Integer = CheckProductAlreadyAdded(ProductId)
    If FoundRowIndex > -1 then
      Dim Quantity as Integer = Convert.ToInt32 (ProdutsGridView.Rows(FoundRowIndex),cells(...
    ...
    Else ' If FoundRowIndex = -1
      ProductsGridView.Rows.Add(ProductID, ...


    thanks for answering to Marry but where I should put the code that offers me I do not understand I'm a novice

    this code is wrong as I fix it I do not understand

     
     Public Function CheckProductAlreadyAdded(ProductID As Integer) As Integer
    
            Dim RowIndex As Integer = -1
            For Each Row As DataGridViewRow In ProductsGridView.Rows
                If Convert.ToInt32(Row.Cells("IdProducto").Value) = ProductID Then
                    RowIndex = Row.Index
                    Return True
                End If
            Next
            Return False
        End Function

    Tuesday, March 13, 2018 12:21 PM
  • does not return true value RowIndex

    Tuesday, March 13, 2018 2:30 PM
  • does not return true value RowIndex

     Sub ProductButton_Click(sender As Object, e As EventArgs)
    
            Dim Producto As New CpE_Cls_PRODUCTO
            Dim objProducto As New CpN_Cls_PRODUCTO
            Dim stock As Integer
            Dim btn2 As Button = CType(sender, Button)
            Dim ProductID As Integer = Convert.ToInt32(btn2.Tag)
            'Dim RowIndex As Integer = Nothing
            Dim FoundRowIndex As Integer = CheckProductAlreadyAdded(ProductID)
    
    
            Try
                ProductDetails = mostrar.BuscarProducto(ProductID)
                Producto.stock = ProductDetails.stock
                stock = Producto.stock
                If FoundRowIndex > -1 Then
    
                    Dim Quantity As Integer = Convert.ToInt32(ProductsGridView.Rows(FoundRowIndex).Cells("Cantidad").Value)
                    Dim Price As Decimal = Convert.ToInt32(ProductsGridView.Rows(FoundRowIndex).Cells("Precio").Value)
                    Quantity += 1
                    If Quantity > stock Then
                        MessageBox.Show("Se ha superado el stock", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                        Exit Sub
                    Else
                        Dim TotalPrice As Double = Convert.ToDouble((Quantity * Price))
                        ProductsGridView.Rows(FoundRowIndex).Cells("Cantidad").Value = Quantity
                        ProductsGridView.Rows(FoundRowIndex).Cells("Importe").Value = TotalPrice
                        TotalBillBox.Text = CalculateTotalBill(ProductsGridView).ToString
                    End If
    
                Else
                    ProductsGridView.Rows.Add(ProductID, ProductDetails.Nombre, ProductDetails.precio, 1, ProductDetails.precio * 1)
                        TotalBillBox.Text = CalculateTotalBill(ProductsGridView).ToString()
    
                    End If
    
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
    
        End Sub


    Tuesday, March 13, 2018 2:51 PM
  • thanks for answering Acamar
    Check the If CheckProductAlreadyAdded (ProductID) if it returns the value of the ProductId

     Public Function CheckProductAlreadyAdded (ProductID As Integer) As Boolean
        
             Dim RowIndex As Integer = Nothing
             For Each Row As DataGridViewRow In ProductsGridView.Rows
                 If Convert.ToInt32 (Row.Cells ("ProductID"). Value) = ProductID Then
                     RowIndex = Row.Index ' this is wrong I do not always say zero
                     Return True
                 End If
             Next
             Return False
         End Function

    but I have a doubt with this line always says zero

      RowIndex = Row.Index



    if I take this line, the code works well, this line makes the products repeat

       If Quantity> = stock Then
                         MessageBox.Show ("The stock has been exceeded", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                         Exit Sub


    One thought on improving this is to use a BindingSource when used you don't have to refer to the DataGridView but instead the BindingSource when searching/locating items either via Find method which returns the position of the found item or -1 if not which can be cast to a DataRowView then access the row property then finally Field property.

    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

    Tuesday, March 13, 2018 3:03 PM
    Moderator
  • thanks for answering Acamar
    Check the If CheckProductAlreadyAdded (ProductID) if it returns the value of the ProductId

     Public Function CheckProductAlreadyAdded (ProductID As Integer) As Boolean
        
             Dim RowIndex As Integer = Nothing
             For Each Row As DataGridViewRow In ProductsGridView.Rows
                 If Convert.ToInt32 (Row.Cells ("ProductID"). Value) = ProductID Then
                     RowIndex = Row.Index ' this is wrong I do not always say zero
                     Return True
                 End If
             Next
             Return False
         End Function

    but I have a doubt with this line always says zero

      RowIndex = Row.Index



    if I take this line, the code works well, this line makes the products repeat

       If Quantity> = stock Then
                         MessageBox.Show ("The stock has been exceeded", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                         Exit Sub


    One thought on improving this is to use a BindingSource when used you don't have to refer to the DataGridView but instead the BindingSource when searching/locating items either via Find method which returns the position of the found item or -1 if not which can be cast to a DataRowView then access the row property then finally Field property.

    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

    He tells me some example how he could do what he says to me please I'm a novice I do not understand much
    Tuesday, March 13, 2018 5:35 PM
  • thanks for answering to Marry but where I should put the code that offers me I do not understand I'm a novice

    Change the CheckProductAlreadyAdded function so that it returns the row number of the item if it is found, or -1 if the row was not found. 

     Public Function CheckProductAlreadyAdded(ProductID As Integer) As Integer
    
            Dim RowIndex As Integer = -1
            For Each Row As DataGridViewRow In ProductsGridView.Rows
                If Convert.ToInt32(Row.Cells("IdProducto").Value) = ProductID Then
                    Return Row.Index
                End If
            Next
            Return -1
        End Function

    Change the main routine so that it uses the return value (a) to check whether or not the row was found and (b) as the row number if the product was found,

    Dim FoundRowIndex as Integer = CheckProductAlreadyAdded(ProductID) If FoundRowIndex > -1 Then Dim Quantity As Integer = Convert.ToInt32(ProductsGridView.Rows(FoundRowIndex).Cells("Cantidad").Value) Dim Price As Decimal = Convert.ToInt32(ProductsGridView.Rows(FoundRowIndex).Cells("Precio").Value) Quantity += 1 '/////////// <Hacer esto ... Importante ... Tener una parte decimal en el precio total> Dim TotalPrice As Double = Convert.ToDouble(Quantity * Price) ProductsGridView.Rows(FoundRowIndex).Cells("Cantidad").Value = Quantity ProductsGridView.Rows(FoundRowIndex).Cells("Importe").Value = TotalPrice TotalBillBox.Text = CalculateTotalBill(ProductsGridView).ToString() If stock >= Quantity Then MessageBox.Show("Se ha superado el stock", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Warning) Exit Sub
    End If Else ' Row was not found. Add it. ProductsGridView.Rows.Add(ProductID, ProductDetails.Nombre, ProductDetails.precio, 1, ProductDetails.precio * 1) TotalBillBox.Text = CalculateTotalBill(ProductsGridView).ToString()

            End If


    • Marked as answer by djnilo Thursday, March 15, 2018 6:00 PM
    Tuesday, March 13, 2018 9:57 PM
  • thanks for answering to Marry but where I should put the code that offers me I do not understand I'm a novice

    Change the CheckProductAlreadyAdded function so that it returns the row number of the item if it is found, or -1 if the row was not found. 

     Public Function CheckProductAlreadyAdded(ProductID As Integer) As Integer
    
            Dim RowIndex As Integer = -1
            For Each Row As DataGridViewRow In ProductsGridView.Rows
                If Convert.ToInt32(Row.Cells("IdProducto").Value) = ProductID Then
                    Return Row.Index
                End If
            Next
            Return -1
        End Function

    Change the main routine so that it uses the return value (a) to check whether or not the row was found and (b) as the row number if the product was found,

    Dim FoundRowIndex as Integer = CheckProductAlreadyAdded(ProductID) If FoundRowIndex > -1 Then Dim Quantity As Integer = Convert.ToInt32(ProductsGridView.Rows(FoundRowIndex).Cells("Cantidad").Value) Dim Price As Decimal = Convert.ToInt32(ProductsGridView.Rows(FoundRowIndex).Cells("Precio").Value) Quantity += 1 '/////////// <Hacer esto ... Importante ... Tener una parte decimal en el precio total> Dim TotalPrice As Double = Convert.ToDouble(Quantity * Price) ProductsGridView.Rows(FoundRowIndex).Cells("Cantidad").Value = Quantity ProductsGridView.Rows(FoundRowIndex).Cells("Importe").Value = TotalPrice TotalBillBox.Text = CalculateTotalBill(ProductsGridView).ToString() If stock >= Quantity Then MessageBox.Show("Se ha superado el stock", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Warning) Exit Sub
    End If Else ' Row was not found. Add it. ProductsGridView.Rows.Add(ProductID, ProductDetails.Nombre, ProductDetails.precio, 1, ProductDetails.precio * 1) TotalBillBox.Text = CalculateTotalBill(ProductsGridView).ToString()

            End If


    Acamar thank you very much for the patience and time that you had with me now works good

    • Marked as answer by djnilo Thursday, March 15, 2018 5:55 PM
    Thursday, March 15, 2018 5:55 PM