Vb.net 如何使用datagridview列减去文本框值..VB

Vb.net 如何使用datagridview列减去文本框值..VB,vb.net,Vb.net,我需要帮助我的vb项目。最后一天我创建了股票数据库 数据库有两列项目名称和数量 在我的表单中,我有两个与数据库相同的文本框和一个按钮 按下按钮时 如果我的“Itemname textbox”与数据库列Itemname匹配,则“Quantity textbox”与“Quantity column”相减 如果与数据库“itemname column”[2]匹配,则第2列的数量减去“Quantitytextbox” 这是我的代码,它可以工作,但很长: If itemname.Text = Me.D

我需要帮助我的vb项目。最后一天我创建了股票数据库

数据库有两列
项目名称
数量

在我的表单中,我有两个与数据库相同的文本框和一个按钮

按下按钮时

  • 如果我的“Itemname textbox”与数据库列
    Itemname
    匹配,则“Quantity textbox”与“Quantity column”相减
  • 如果与数据库“itemname column”[2]匹配,则第2列的数量减去“Quantitytextbox”
这是我的代码,它可以工作,但很长:

If itemname.Text = Me.DataGridView1.Rows(0).Cells(0).Value Then
            Me.DataGridView1.Rows(0).Cells(1).Value = CDbl(Me.DataGridView1.Rows(0).Cells(1).Value) - CDbl(quantity.Text)
            MsgBox("Data Saved SuccessFully")
            quantity.Text = ""
            itemname.Text = ""

        ElseIf itemname.text = Me.DataGridView1.Rows(1).Cells(0).Value Then
            Me.DataGridView1.Rows(1).Cells(1).Value = CDbl(quantity.Text) - CDbl(Me.DataGridView1.Rows(1).Cells(1).Value)
            MsgBox("Data Saved SuccessFully")
            quantity = ""
            itemname.Text = ""

        ElseIf itemname.Text = Me.DataGridView1.Rows(2).Cells(0).Value Then
            Me.DataGridView1.Rows(2).Cells(1).Value = CDbl(quantity.Text) - CDbl(Me.DataGridView1.Rows(2).Cells(1).Value)
            MsgBox("Data Saved SuccessFully")
            quantity = ""
            itemname.Text = ""


        ElseIf itemname.Text = Me.DataGridView1.Rows(3).Cells(0).Value Then
            Me.DataGridView1.Rows(3).Cells(1).Value = CDbl(quantity.Text) - CDbl(Me.DataGridView1.Rows(3).Cells(1).Value)
            MsgBox("Data Saved SuccessFully")
            quantity.Text = ""
            itemname.Text = ""

          endif
end sub
等等


请帮助….

使用
For…Next
循环遍历以下行:

 Dim intRow As Integer
 'loop through the rows, looking for a matching record
 For intRow = 0 To DataGridView1.Rows.Count - 1
   If itemname.Text = Me.DataGridView1.Rows(intRow).Cells(0).Value Then
     Me.DataGridView1.Rows(intRow).Cells(1).Value = CDbl(Me.DataGridView1.Rows(intRow).Cells(1).Value) - CDbl(quantity.Text)
     MsgBox("Data Saved SuccessFully")
     quantity.Text = ""
     itemname.Text = ""
     Exit For 'exit the loop
   End If
 Next intRow
使用变量
intRow
,而不是键入每个行号