我正在开发一个保存股票的计划。如果ShoeID和Size已经被删除,我需要更新表的库存。否则它应该添加一个新行。
当我输入数据时,如果在之前的记录中有相同的shoeID和Size,则会更新。但是,如果我在一些记录之后添加记录,它会创建一个新记录。不更新以前的记录。
Private Sub AddStockBtn_Click(sender As Object, e As EventArgs) Handles AddStockBtn.Click
Dim Size, Stock As Integer
Dim stock_check As String
Dim status As Boolean = False
stock_check = "Pending"
StockBindingSource.ResetBindings(True)
Try
Size = Integer.Parse(SizeTxt.Text)
Stock = Integer.Parse(StockTxt.Text)
Console.WriteLine(stock_check)
Catch ex As Exception
MessageBox.Show("Invalid Size or Stock")
End Try
If (StockBindingSource.Count = 0) Then
StockBindingSource.AddNew()
StockBindingSource.Current("No") = 1
StockBindingSource.Current("ShoeID") = IDBox.Text
For i As Integer = 0 To ShoeDataBindingSource.Count - 1
Dim rowData As DataRowView = ShoeDataBindingSource.Item(i)
If rowData("ShoeID").ToString = IDBox.Text Then
StockBindingSource.Current("ShoeType") = ShoeDataBindingSource.Current("Type")
StockBindingSource.Current("Description") = ShoeDataBindingSource.Current("Name")
End If
Next
StockBindingSource.Current("Size") = Size
StockBindingSource.Current("Stock") = Stock
StockBindingSource.EndEdit()
TableAdapterManager.UpdateAll(SilexDatabaseDataSet)
Else
For i As Integer = 0 To ShoeDataBindingSource.Count - 1
Dim rowName As DataRowView = ShoeDataBindingSource.Item(i)
If rowName("ShoeID").ToString = IDBox.Text And StockBindingSource.Current("Size") = Size Then
StockBindingSource.Current("Stock") = StockBindingSource.Current("Stock") + Stock
StockBindingSource.EndEdit()
TableAdapterManager.UpdateAll(SilexDatabaseDataSet)
status = True
End If
Console.WriteLine(ShoeDataBindingSource.Count)
stock_check = "Loop"
Next
If (Not status And stock_check = "Loop") Then
Dim no As Integer
no = StockBindingSource.Count + 1
StockBindingSource.AddNew()
StockBindingSource.Current("No") = no
StockBindingSource.Current("ShoeID") = IDBox.Text
For i As Integer = 0 To ShoeDataBindingSource.Count - 1
Dim rowData As DataRowView = ShoeDataBindingSource.Item(i)
If rowData("ShoeID").ToString = IDBox.Text Then
StockBindingSource.Current("ShoeType") = ShoeDataBindingSource.Current("Type")
StockBindingSource.Current("Description") = ShoeDataBindingSource.Current("Name")
End If
Next
StockBindingSource.Current("Size") = Size
StockBindingSource.Current("Stock") = Stock
StockBindingSource.EndEdit()
TableAdapterManager.UpdateAll(SilexDatabaseDataSet)
End If
End If
End Sub
希望你能理解这个问题。有人帮忙。
StockBindingSource.count不会更新。它总是显示为1,除非它为空。
答案 0 :(得分:0)
请参阅上面有关我尝试将此代码发布到评论中的评论。
Dim ShoeId As Integer = 1
Dim ShoeSize As Integer = 15
Dim Stock As Integer = 1
Dim thisRow As DataRow = Nothing
Dim dt As New DataTable
dt.Columns.Add(New DataColumn With
{
.ColumnName = "Id",
.DataType = GetType(Integer),
.AutoIncrement = True,
.AutoIncrementSeed = 1
})
dt.Columns.Add(New DataColumn With
{
.ColumnName = "ShoeId",
.DataType = GetType(Integer),
.AutoIncrement = True
})
dt.Columns.Add(New DataColumn With
{
.ColumnName = "Size",
.DataType = GetType(Integer)
})
dt.Columns.Add(New DataColumn With
{
.ColumnName = "ShoeType",
.DataType = GetType(String)
})
dt.Columns.Add(New DataColumn With
{
.ColumnName = "Stock",
.DataType = GetType(Integer)
})
dt.Rows.Add(New Object() {Nothing, 1, 15, "Shoe", 5})
bs.DataSource = dt
thisRow =
(
From t In CType(bs.DataSource, DataTable).AsEnumerable
Where t.Field(Of Integer)("ShoeId") = ShoeId AndAlso t.Field(Of Integer)("Size") = ShoeSize
Select t).FirstOrDefault
If thisRow IsNot Nothing Then
thisRow.SetField(Of Integer)("Stock", thisRow.Field(Of Integer)("Stock") + 1)
Else
' add
End If
ShoeId = 2
ShoeSize = 15
thisRow =
(
From t In CType(bs.DataSource, DataTable).AsEnumerable
Where t.Field(Of Integer)("ShoeId") = ShoeId AndAlso t.Field(Of Integer)("Size") = ShoeSize
Select t).FirstOrDefault
If thisRow IsNot Nothing Then
thisRow.SetField(Of Integer)("Stock", thisRow.Field(Of Integer)("Stock") + 1)
Else
CType(bs.DataSource, DataTable).Rows.Add(New Object() {Nothing, ShoeId, ShoeSize, "Shoe", 5})
End If
ShoeId = 1
ShoeSize = 15
thisRow =
(
From t In CType(bs.DataSource, DataTable).AsEnumerable
Where t.Field(Of Integer)("ShoeId") = ShoeId AndAlso t.Field(Of Integer)("Size") = ShoeSize
Select t).FirstOrDefault
If thisRow IsNot Nothing Then
thisRow.SetField(Of Integer)("Stock", thisRow.Field(Of Integer)("Stock") + 1)
Else
CType(bs.DataSource, DataTable).Rows.Add(New Object() {Nothing, ShoeId, ShoeSize, "Shoe", 5})
End If
答案 1 :(得分:0)
最后,我找到了一种简单的方法来完成任务。谢谢Karen的帮助。
For Each row As DataGridViewRow In StockDataGrid.Rows
Dim tempID As String = row.Cells(1).Value
Dim tempSize As String = row.Cells(3).Value
If tempID = IDBox.Text And tempSize = Size Then
StockBindingSource.Position = count
StockBindingSource.Current("Stock") = StockBindingSource.Current("Stock") + Stock
StockBindingSource.EndEdit()
TableAdapterManager.UpdateAll(SilexDatabaseDataSet)
status = True
End If
count = count + 1
Next
这样,我可以解决我的问题。我使用了datagridview。