我的vb项目需要帮助。最后一天我创建了股票数据库
数据库有2列Item Name
和Quantity
在我的表单中,我有两个与数据库相同的文本框和一个按钮
按下按钮时
itemname
然后"数量文本框"减去"数量列" 这是我的代码,但它很长:
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
等等.......
请帮助......
答案 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
而不是键入每个行号。