如何使用datagridview column..VB减去文本框值

时间:2014-05-02 00:00:35

标签: vb.net

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

数据库有2列Item NameQuantity

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

按下按钮时

  • 如果我的"项目名称文本框"匹配数据库列itemname然后"数量文本框"减去"数量列"
  • 如果与数据库匹配" itemname列" [2]则将column2数量减去" 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

等等.......

请帮助......

1 个答案:

答案 0 :(得分:0)

使用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而不是键入每个行号。