locked
how can i Subtract from datagridview RRS feed

  • Question

  • I have a label that has a total amount for the product
    I want to give some discount in this which will be through datagridview 
    How can i do this.
    Friday, July 10, 2020 10:52 AM

All replies

  • Apply discount for each item

    Private Discount As Double = 25
    Private Sub ApplyDiscountButton_Click(sender As Object, e As EventArgs) Handles ApplyDiscountButton.Click
        For rowIndex As Integer = 0 To DataGridView1.Rows.Count - 1
            If Not DataGridView1.Rows(rowIndex).IsNewRow Then
                Dim originalValue = Convert.ToDouble(DataGridView1.Rows(rowIndex).Cells(1).Value)
                DataGridView1.Rows(rowIndex).Cells(1).Value = originalValue - (originalValue * Discount / 100)
            End If
        Next
    End Sub

    if you want to simply do a total.

    Private Discount As Double = 25
    Private Sub ApplyDiscountButton_Click(sender As Object, e As EventArgs) Handles ApplyDiscountButton.Click
    
        Dim originalValues = DataGridView1.
                Rows.Cast(Of DataGridViewRow).
                Select(Function(row) Convert.ToDouble(row.Cells(1).Value)).
                Sum()
    
        Dim total = originalValues - (originalValues * Discount / 100)
        Console.WriteLine(originalValues)
        Console.WriteLine(total)
    
    End Sub
    • Note I used no DataSource for the DataGridView, if there is a DataSource you can still use the above or adapt to cast to the DataSource.
    • I used type Double, if not double than change to your type e.g. decimal, integer etc.
    • Console.WriteLine would be replaced with a TextBox, Label or add a new row in the DataGridView and set the discount value (if you add more rows this becomes problematic, needs different code).
    • I assume the value for each cell is not empty.

    Here I use a label.

    Dim total = originalValues - (originalValues * Discount / 100)
    Label1.Text = total.ToString("C2")


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Friday, July 10, 2020 11:27 AM
  • Hello,

    How are you doing with this task?


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, July 11, 2020 10:49 AM