当 NumericUpDown 的值为零时删除 DataGridView 中的一行
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
根据屏幕截图,我认为您想在 DataGridView 的数量设置为零时从 DataGridView 中删除项目行,如果是这样的话:
您必须首先查看 DGV 中的行索引,然后通过执行以下操作将其删除:
创建子项以查找相关行并将其删除
从 Numeric 对象的 ValueChanged 事件调用此子,如下所示:
Private Sub NumericUpDown1_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown1.ValueChanged
If NumericUpDown1.Value = 0 Then
DeleteRowFromDGV("Hawaiian")
End If
End Sub
Private Sub DeleteRowFromDGV(itemName As String)
With DataGridView1
Dim rowToDelete As Short
For i As Short = 0 To .RowCount - 1
If .Item(0, i).Value = itemName Then 'suppose that the column index of item name is 0
rowToDelete = i
Exit For
End If
Next
.Rows.RemoveAt(rowToDelete)
End With
End Sub
你把事情搞得比原来复杂得多。为了简化事情,我建议您创建一个采用三 (3) 个参数的方法; string
披萨名称、decimal
数量和 decimal
价格。
在此方法中,第一步是检查数量是否为零。如果它为零,则遍历网格中的所有行,直到找到比萨饼名称。如果未找到披萨名称,那么我们可以 return 因为该行不是开头的。如果找到该行,我们将删除该行。
如果数量不为零,那么我们将遍历网格中的所有行,直到找到披萨名称。如果未找到披萨名称,那么我们知道我们想要将行“添加”到网格中。如果找到披萨名称,则更新数量和金额单元格。
这个方法可能看起来像……
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
此外,由于表单上有许多 NUD (NumericUpDown
) 控件,并且每个 NUD 都与特定的比萨饼相关联,因此“每个”NUD 都没有一个 ValueChanged
事件,我建议您创建一 (1) 个 ValueChanged
事件并让“每个”NUD 订阅同一事件。当事件触发时,我们将检查“哪个”NUD 正在更改其值,然后使用正确的信息简单地调用上述方法。这应该可以将您必须管理的事件数量减少到所有 NUD 的一个 ValueChanged
事件。
为了使代码更具可读性,我建议您为每个 NUD 起一个更符合逻辑的名称,例如 NUD_Pepperoni
、NUD_Hawaiian
等...因为我们将使用 NUD Name
属性 来识别“哪些”NUD 值发生了变化。使用上述方法的事件可能看起来像……
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
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
根据屏幕截图,我认为您想在 DataGridView 的数量设置为零时从 DataGridView 中删除项目行,如果是这样的话:
您必须首先查看 DGV 中的行索引,然后通过执行以下操作将其删除:
创建子项以查找相关行并将其删除 从 Numeric 对象的 ValueChanged 事件调用此子,如下所示:
Private Sub NumericUpDown1_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown1.ValueChanged
If NumericUpDown1.Value = 0 Then
DeleteRowFromDGV("Hawaiian")
End If
End Sub
Private Sub DeleteRowFromDGV(itemName As String)
With DataGridView1
Dim rowToDelete As Short
For i As Short = 0 To .RowCount - 1
If .Item(0, i).Value = itemName Then 'suppose that the column index of item name is 0
rowToDelete = i
Exit For
End If
Next
.Rows.RemoveAt(rowToDelete)
End With
End Sub
你把事情搞得比原来复杂得多。为了简化事情,我建议您创建一个采用三 (3) 个参数的方法; string
披萨名称、decimal
数量和 decimal
价格。
在此方法中,第一步是检查数量是否为零。如果它为零,则遍历网格中的所有行,直到找到比萨饼名称。如果未找到披萨名称,那么我们可以 return 因为该行不是开头的。如果找到该行,我们将删除该行。
如果数量不为零,那么我们将遍历网格中的所有行,直到找到披萨名称。如果未找到披萨名称,那么我们知道我们想要将行“添加”到网格中。如果找到披萨名称,则更新数量和金额单元格。
这个方法可能看起来像……
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
此外,由于表单上有许多 NUD (NumericUpDown
) 控件,并且每个 NUD 都与特定的比萨饼相关联,因此“每个”NUD 都没有一个 ValueChanged
事件,我建议您创建一 (1) 个 ValueChanged
事件并让“每个”NUD 订阅同一事件。当事件触发时,我们将检查“哪个”NUD 正在更改其值,然后使用正确的信息简单地调用上述方法。这应该可以将您必须管理的事件数量减少到所有 NUD 的一个 ValueChanged
事件。
为了使代码更具可读性,我建议您为每个 NUD 起一个更符合逻辑的名称,例如 NUD_Pepperoni
、NUD_Hawaiian
等...因为我们将使用 NUD Name
属性 来识别“哪些”NUD 值发生了变化。使用上述方法的事件可能看起来像……
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