2015-12-21 37 views
0

我正在開發一個程序來保存股票。如果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 

我在這裏輸入我的數據第一次 First Data enter

更新相同的數據 Second

添加另一個數據,並再次增加相同的數據, enter image description here

新數據更新 enter image description here

希望你明白這個問題。有人請幫忙。

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 
+0

非常感謝您的回覆。真的很感激它。你能解釋一下'bs.DataSource = dt'是什麼。 – Choxmi

+0

bs是一個BindingSource。我將DataSource設置爲一個名爲dt的DataTable變量。如前所述,這是概念性的,因爲我將一堆代碼放在一起,這些代碼通常會在不同的方法中,這就是爲什麼我將BindingSource的DataSource轉換爲即使您可見的情況,但是當dt作爲變量不可見時,您需要執行此操作。希望這可以幫助。 –

+0

謝謝凱倫。但在我的代碼中,我只是使用數據集的默認綁定源,我需要更新數據集。如果存在相同的ID和大小,則新值應替換舊值,如果不是,則會創建新記錄。我試着用你的代碼。但我不能那樣做。 :( – Choxmi

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。

相關問題