BindingSource.Count 除非不为空 VB.Net 否则始终显示为 1
BindingSource.Count always display as 1 unless it is not null VB.Net
我正在开发一个程序来保持库存。如果 ShoeID 和尺码已经 excisting.Otherwise,我需要更新 table 的库存,它应该添加一个新行。
当我输入数据时,如果与之前记录的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,除非它为空。
请参阅我上面关于我试图 post 将此代码添加到评论中的评论。
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
我终于找到了一个简单的方法来完成任务。感谢凯伦的帮助。
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。
我正在开发一个程序来保持库存。如果 ShoeID 和尺码已经 excisting.Otherwise,我需要更新 table 的库存,它应该添加一个新行。
当我输入数据时,如果与之前记录的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,除非它为空。
请参阅我上面关于我试图 post 将此代码添加到评论中的评论。
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
我终于找到了一个简单的方法来完成任务。感谢凯伦的帮助。
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。