所以下面的代码如果条形码字符长度小于11,则通过从表单2中的datagridview中选择复选框,然后将datatable填充到表单1中的datagridview来手动完成。但是我还没有成功,我的代码有问题吗?请指导
谢谢
form1中的代码
Public Class Form1
Private Sub FillDataTable(iRow As Integer, ByVal Codeproduct As String, Barcode As String, ColorCode As String, Qty As Integer)
Dim row As DataRow = _myTable.NewRow()
row("No") = iRow
row("Codeproduct") = Codeproduct
row("Barcode") = Barcode
row("ColorCode") = ColorCode
row("Qty") = Qty
_myTable.Rows.Add(row)
End Sub
Private Sub BtxtBarcode_KeyPress(sender As Object, e As KeyPressEventArgs) Handles BtxtBarcode.KeyPress
Try
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then
If BtxtBarcode.Text = "" Then
MsgBox("Please enter item barcode.", MsgBoxStyle.Information, "Information")
Else
If BtxtBarcode.TextLength <= 11 Then
GetItemData(BtxtBarcode.Text)
Dim iRow As Integer
If Grid.RowCount - 1 = 0 Then
iRow = 1
Else
iRow = Convert.ToInt32(Grid.Rows(Grid.RowCount - 2).Cells(0).Value.ToString()) + 1
End If
Dim Found As Boolean = False
Dim newValue As Integer = 1
For j = 0 To Me.Grid.Rows.Count - 1
If Convert.ToString(Me.Grid.Rows(j).Cells(1).Value) = BtxtBarcode.Text.ToString() Then
Found = True
If Integer.TryParse(Grid.Rows(j).Cells(4).Value, newValue) Then
newValue += 1
Else
newValue = 1
End If
Grid.Rows(j).Cells(4).Value = newValue.ToString()
BtxtBarcode.Clear()
Exit For
End If
Next
If Not Found Then
'below is the filldatatable method code and update code progress
Dim f2 As New Form2
f2.ShowDialog()
Dim columntwo = f2.DataGridView1.CurrentRow.Cells(2).Value.ToString
Dim columnthree = f2.DataGridView1.CurrentRow.Cells(3).Value.ToString
FillDataTable(iRow, columntwo, _BBarcode, columnthree, newValue)
Grid.DataSource = _myTable
BtxtBarcode.Clear()
End If
End If
End If
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "POS", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.Show()
End Sub
End Class
Public Class Stocks
Public Property Process() As Boolean
Public Property Id() As Integer
Public Property CodeProduct() As String
Public Property Colorcode() As String
Public Property Size() As String
Public Property Qty_Stock() As Integer
End Class
Public Class item
Public Property No() As Integer
Public Property CodeProduct() As String
Public Property Barcode() As String
Public Property Colorcode() As String
End Class
form2中的代码
Public Class Form2
Private iService As New Itemservice2()
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView1.DataSource = iService.Getitem2(Form1.Btxtcodeproduct.Text)
DataGridView1.Columns(0).Visible = False
End Sub
End Class
Public Class Itemservice2
Private ReadOnly _conn As OleDbConnection
Private _connectionString As String = DbContext.GetOledbConnectionString()
Public Function Getitem2(ByVal code As String) As IEnumerable(Of Stocks)
Dim sql = $"SELECT * FROM Stocks WHERE CodeProduct = '{code}'"
Using _conn = New OleDbConnection(DbContext.GetOledbConnectionString())
Return _conn.Query(Of Stocks)(sql).ToList()
End Using
End Function
Public Function GetByCodeProductOrBarcode(ByVal code As String) As item
Dim sql = $"SELECT Items.Barcode,Items.CodeProduct,Stocks.Colorcode,Stocks.Size FROM Items INNER JOIN Stocks ON Items.Barcode = Stocks.Barcode WHERE Items.CodeProduct = '{code}' or Items.Barcode = '{code}'"
Using _conn = New OleDbConnection(DbContext.GetOledbConnectionString())
Return _conn.Query(Of item)(sql).FirstOrDefault()
End Using
End Function
End Class
表格 1 中 datagridview 中的所需结果
没有 | 代码产品 | 条形码 | 颜色代码 | 数量 |
---|---|---|---|---|
1 | KBE 0927 | 03-22164010 | BHTF07KP | 1 |
表格 1 中的代码
Public Class Form1
Protected Overrides Sub OnLoad(e As EventArgs)
MyBase.OnLoad(e)
_myTable = New DataTable
_myTable.Columns.AddRange({
New DataColumn("No", GetType(Integer)),
New DataColumn("Codeproduct", GetType(String)),
New DataColumn("Barcode", GetType(String)),
New DataColumn("ColorCode", GetType(String)),
New DataColumn("Qty", GetType(Integer))
})
Grid.DataSource = _myTable
End Sub
Private Sub BtxtBarcode_KeyPress(sender As Object,
e As KeyPressEventArgs) _
Handles BtxtBarcode.KeyPress
If e.KeyChar = Convert.ToChar(Keys.Enter) Then
AddEntry()
End If
End Sub
Private Sub AddEntry()
If String.IsNullOrWhiteSpace(BtxtBarcode.Text) OrElse
String.IsNullOrWhiteSpace(Btxtcodeproduct.Text) Then
MsgBox("Please enter item barcode or product code...")
Return
End If
Dim productCode = Btxtcodeproduct.Text.Trim()
Dim row As DataRow
Dim _BBarcode = BtxtBarcode.Text.Trim()
If _myTable.Rows.Cast(Of DataRow).Any() Then
If BtxtBarcode.TextLength <= 10 Then
Using frm = New Form2(productCode)
If frm.ShowDialog() = DialogResult.OK Then
End If
End Using
row = _myTable.
Select($"Codeproduct = '{productCode}'").
FirstOrDefault()
Else
row = _myTable.
Select($"Barcode = '{_BBarcode}'").
FirstOrDefault()
End If
' Change the length as needed...
If BtxtBarcode.TextLength >= 11 Then
row = _myTable.
Select($"Codeproduct = '{productCode}'").
FirstOrDefault()
Else
row = _myTable.
Select($"Barcode = '{_BBarcode}'").
FirstOrDefault()
End If
If row IsNot Nothing Then
Dim qty = If(row("Qty") Is DBNull.Value, 1, row.Field(Of Integer)("Qty"))
row.SetField("Qty", qty + 1)
Return
End If
End If
Try
Dim service = New Itemservice2()
Dim stock = service.Getitem2(productCode).FirstOrDefault()
If stock Is Nothing Then
MessageBox.Show("Unknown product!")
Return
End If
' Where does _BBarcode come from?
_myTable.Rows.Add(_myTable.Rows.Count + 1, productCode,
_BBarcode, stock.Colorcode, 1)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
End Class
表格2中的代码
Public Class Form2
Sub New()
InitializeComponent()
End Sub
' A constructor with a parameter...
Public Sub New(productCode As String)
Me.New
' Moved from Form2_Load event.
DataGridView1.DataSource = iService.Getitem2(productCode)
DataGridView1.Columns(0).Visible = False
End Sub
Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
DialogResult = DialogResult.OK
End Sub
End Class
事实证明,您正在尝试
Form1
增加产品的数量,而不是添加新条目。通过有效的 Barcode
或 Codeproduct
在网格中搜索产品。但如果网格是空的,那么这当然不起作用,因此会出现 Form2
部分。您想要显示 Form2
,用 List<Stocks>
填充其网格,通过 Codeproduct
查找产品(Stocks 模型不包含 Barcode
属性),并从网格本身提取产品的属性。
不,您不需要这样做。您根本不需要
Form2
部分。您有 Itemservice2
类,它公开了 Getitem2
方法,该方法执行数据库查询以通过 CodeProduct
字段选择重新编码。那么,为什么不在 Form1
本身中这样做呢?
例如,在
Form1
Private _myTable As DataTable
Protected Overrides Sub OnLoad(e As EventArgs)
MyBase.OnLoad(e)
_myTable = New DataTable
_myTable.Columns.AddRange({
New DataColumn("No", GetType(Integer)),
New DataColumn("Codeproduct", GetType(String)),
New DataColumn("Barcode", GetType(String)),
New DataColumn("ColorCode", GetType(String)),
New DataColumn("Qty", GetType(Integer))
})
Grid.DataSource = _myTable
End Sub
Private Sub BtxtBarcode_KeyPress(sender As Object,
e As KeyPressEventArgs) _
Handles BtxtBarcode.KeyPress
If e.KeyChar = Convert.ToChar(Keys.Enter) Then
AddEntry()
End If
End Sub
Private Sub AddEntry()
If String.IsNullOrWhiteSpace(BtxtBarcode.Text) OrElse
String.IsNullOrWhiteSpace(BtxtCodeProduct.Text) Then
MsgBox("Please enter item barcode or product code...", ...
Return
End If
Dim productCode = BtxtCodeProduct.Text.Trim()
If _myTable.Rows.Cast(Of DataRow).Any() Then
Dim row As DataRow
' Change the length as needed...
If BtxtBarcode.TextLength <> 11 Then
row = _myTable.
Select($"Codeproduct = '{productCode}'").
FirstOrDefault()
Else
Dim barcode = BtxtBarcode.Text.Trim()
row = _myTable.
Select($"Barcode = '{barcode}'").
FirstOrDefault()
End If
If row IsNot Nothing Then
Dim qty = If(row("Qty") Is DBNull.Value, 1, row.Field(Of Integer)("Qty"))
row.SetField("Qty", qty + 1)
Return
End If
End If
Try
Dim service = New Itemservice2()
Dim stock = service.Getitem2(productCode).FirstOrDefault()
If stock Is Nothing Then
MessageBox.Show("Unknown product!")
Return
End If
' Where does _BBarcode come from?
_myTable.Rows.Add(_myTable.Rows.Count + 1, productCode,
_BBarcode, stock.Colorcode, 1)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
这里注意,当你有数据源时,然后处理它来操作数据而不是控件本身。该控件的业务是以某种方式可视化该数据源中的内容。
DataTable.Select
方法来搜索 Barcode
或 Codeproduct
字段(如果 DataTable
不为空)进行匹配,并选择第一个 DataRow
(如果有)以增加 Qty
字段。
如果
DataTable
为空或 Select
方法由于某种原因没有返回任何内容,则 Try ... End Try
块中的下一段代码将运行并在数据库中搜索该产品。如果有的话,您将获得一个 Stocks
对象,从中获取所需的详细信息,并将新的 DataRow
添加到您的 _myTable
。
对于您的代码片段,要在显示时自动填充
Form2
的网格,请添加一个采用产品标识符的构造函数重载:
Public Class Form2
' The default constructor...
Sub New()
InitializeComponent()
End Sub
' A constructor with a parameter...
Public Sub New(productCode As String)
Me.New
' Moved from Form2_Load event.
DataGridView1.DataSource = iService.Getitem2(productCode)
DataGridView1.Columns(0).Visible = False
End Sub
' ...etc.
End Class
在
Form1
中,创建一个新的 Form2
并将该产品代码传递给构造函数。例如:
Dim productCode = BtxtCodeProduct.Text.Trim()
Using frm = New Form2(productCode)
If frm.ShowDialog() = DialogResult.OK Then
' ...etc.
End If
End Using
Form2
必须将 DialogResult.OK
值返回到 Form1
才能继续,或将 DialogResult.Cancel
返回到 Return
或 Exit Sub
。为此添加 OK
和 Canel
按钮。
Private Sub btnOK_Click(sender As Object, e As EventArgs) _
Handles btnOK.Click
DialogResult = DialogResult.OK
End Sub
现在,
AddEntry
中的Form1
方法应该是这样的。
Private Sub AddEntry()
If String.IsNullOrWhiteSpace(BtxtBarcode.Text) AndAlso
String.IsNullOrWhiteSpace(BtxtCodeProduct.Text) Then
MsgBox("Please enter item barcode or product code...", ...
Return
End If
Dim productCode = BtxtCodeProduct.Text.Trim()
Dim _BBarcode = BtxtBarcode.Text.Trim()
If (_BBarcode.Length < 11) Then
Using frm = New Form2(productCode)
If frm.ShowDialog() = DialogResult.OK Then
' Get what you want from Form2...
End If
End Using
Else ' _BBarcode.Length >= 11
Dim row As DataRow = Nothing
If _myTable.Rows.Cast(Of DataRow).Any() Then
row = _myTable.Select($"Barcode = `{_BBarcode}` Or Codeproduct = '{productCode}'").FirstOrDefault()
End If
If row IsNot Nothing Then
Dim qty = If(row("Qty") Is DBNull.Value, 0, row.Field(Of Integer)("Qty"))
row.SetField("Qty", qty + 1)
Else
' No matches were found in Form1's grid. What now?
' Exit Sub ?
' row = _myTable.NewRow and add some values then
' _myTable.Rows.Add(row) ?
' It's up to you!
End If
End If
End Sub