Search code examples
vb.netdatagridviewdelete-row

Delete a row in DataDridView when a value of NumericUpDown equal to zero


For Each row As DataGridViewRow In DataGridView1.Rows
        If row.Cells(0).Value = "Hawaiian" Then
            row.Cells(1).Value = Double.Parse(NumericUpDown1.Value)
            row.Cells(2).Value = Double.Parse(row.Cells(1).Value) * price
            Exit Sub
        End If
    Next
    DataGridView1.Rows.Add("Hawaiian", 1, price)

the code I put in NumericUpDown to add the item to DataGridView1 is above

I want the row to be deleted when Qty or Amount equal to zero, in this case, is the row for Hawaiian be delete

 If NumericUpDown1.Value = 0 Then
    DataGridView1.Rows.RemoveAt(DataGridView1.CurrentRow.Index)
     End If 

I tried this but it only deleted the first row in DataGridView1. Please help me if you know the answer, I really appreciate it. I have been trying to solve this for a week, used so many methods online all not work, I still have no clue.

Expand: the code I added from the comment, it's not deleting

The code


Solution

  • You are making this far more complicated than it has to be. To simplify things, I suggest you create a method that takes three (3) parameters; a string pizza name, a decimal quantity and a decimal price.

    Inside this method, the first step is to check to see if quantity is zero. If it is zero, then loop through all the rows in the grid until we find the pizza name. If the pizza name is NOT found then we can return as the row is not there to begin with. If the row IS found, then we will delete that row.

    If the quantity is NOT zero, then we will loop through all the rows in the grid until we find the pizza name. If the pizza name is NOT found, then we know we want to “add” the row to the grid. If the pizza name IS found, then update the quantity and amount cells.

    This method may look something like…

    Private Sub AddOrRemovePizzaInGrid(targetPizza As String, quantity As Decimal, price As Decimal)
      If quantity = 0 Then
        For Each row As DataGridViewRow In DataGridView1.Rows
          If row.Cells("Item").Value IsNot Nothing Then
            If row.Cells("Item").Value.ToString().Equals(targetPizza) Then
              DataGridView1.Rows.Remove(row)
              Return
            End If
          End If
        Next
        Return
      End If
      ' quantity is not zero
      For Each row As DataGridViewRow In DataGridView1.Rows
        If Not row.IsNewRow Then
          If row.Cells("Item").Value IsNot Nothing Then
            If row.Cells("Item").Value.ToString().Equals(targetPizza) Then
              row.Cells("Qty").Value = quantity
              row.Cells("Amount").Value = quantity * price
              Return
            End If
          End If
        End If
      Next
      ' if we get here - the target pizza was not found so add as new row
      DataGridView1.Rows.Add(targetPizza, quantity, quantity * price)
    End Sub
    

    In addition, since there are numerous NUD (NumericUpDown) controls on the form and each NUD is tied to a specific pizza, then instead of having a ValueChanged event for “each” NUD, I suggest you create ONE (1) ValueChanged event and have “each” NUD subscribe to this same event. When the event fires, we will check to see “which” NUD is changing its value, then simply call the above method with the proper info. This should reduce the number of events you have to manage down to one ValueChanged event for all the NUDs.

    To make the code more readable I suggest you give each NUD a more logical name like NUD_Pepperoni, NUD_Hawaiian etc… as we will use the NUDs Name property to identify “which” NUDs value has changed. This event that uses the above method may look something like…

    Private Sub NUD_ValueChanged(sender As Object, e As EventArgs) Handles NUD_Pepperoni.ValueChanged, NUD_Hawaiian.ValueChanged, NUD_Americano.ValueChanged
      Dim target_NUD As NumericUpDown = CType(sender, NumericUpDown)
      Select Case target_NUD.Name
        Case "NUD_Pepperoni"
          AddOrRemovePizzaInGrid("Pepperoni", target_NUD.Value, 8.5D)
        Case "NUD_Hawaiian"
          AddOrRemovePizzaInGrid("Hawaiian", target_NUD.Value, 8.5D)
        Case "NUD_Americano"
          AddOrRemovePizzaInGrid("Americano", target_NUD.Value, 8.5D)
          ' add more pizza types here
      End Select
    End Sub