我正在開發一個程序來保存股票。如果ShoeID和Size已經令人興奮,我需要更新表格的庫存。否則,它應該添加一個新行。BindingSource.Count總是顯示爲1,除非它不爲空VB.Net
當我輸入數據時,如果鞋號和尺碼在先前的記錄中相同,它會更新。但是如果我在一些記錄後添加記錄,它會創建一條新記錄。不更新以前的記錄。
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,除非它爲空。
最好嘗試設置斷點,調試代碼以查看是否可以檢測到問題。從概念上講,應該從底層數據源中解脫出來。我試圖在這裏粘貼這個例子,但它太長了,所以我把它作爲一個答案發布,即使你不是。 –