Why doesn't qty column increment when adding in textbox to datagridview in vb.net.
so the qty column can only max the value to 2 is there something wrong with my code should continue to grow if the product code is the same as the product column in the DataGridView.
Thanks
Private Sub BindItemDetail()
If _myTable.Rows.Count = 0 Then
Dim field() As String = {"No", "Codeproduct", "Barcode", "Qty"}
_myTable = DataControl.CreateDataTableDynamic(field)
End If
grid.DataSource = _myTable
End Sub
Private Sub FillDataTable(iRow As Integer, ByVal Codeproduct As String, ByVal Barcode As String, ByVal Qty As Integer, ByVal Coledit As String, ByVal ColDel As String)
Dim row As DataRow = _myTable.NewRow()
row("No") = iRow
row("Codeproduct") = Codeproduct
row("Barcode") = Barcode
row("Qty") = Qty
_myTable.Rows.Add(row)
End Sub
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
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 Qty As Integer = 1
Dim Found As Boolean = False
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then
For j = 0 To Me.Grid.Rows.Count - 1
If Convert.ToString(Me.Grid.Rows(j).Cells(1).Value) = TextBox1.Text.ToString() Then
Found = True
'The problem in my code is in below the code line
Grid.Rows(j).Cells(3).Value = Qty + 1
TextBox1.Clear()
TextBox2.Clear()
Exit For
End If
Next
If Not Found Then
FillDataTable(iRow, TextBox1.Text, TextBox2.Text, Qty)
Grid.DataSource = _myTable
TextBox1.Clear()
TextBox2.Clear()
End If
End If
End Sub
Desired result
No | CodeProduct | Barcode | Qty |
---|---|---|---|
1 | 1000 | 1000 | 3 |
2 | 1002 | 1002 | 1 |
The problem with the code is you are setting the cell value to 2, not incrementing it. You set this:
Dim Qty As Integer = 1
and then do this:
Grid.Rows(j).Cells(3).Value = Qty + 1
So if you actually want to increment the cell value by 1, then you would instead use this:
Dim newValue As Integer
If Integer.TryParse(Grid.Rows(j).Cells(3).Value, newValue) Then
newValue += 1
Else
newValue = 1
End If
Grid.Rows(j).Cells(3).Value = newValue.ToString()
and not define the Qty
variable at all.
While at it, you probably want to check for the Enter key first, since it seems you don't want to do anything else in this method for other keys. This would be the revised code:
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then
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
For j = 0 To Me.Grid.Rows.Count - 1
If Convert.ToString(Me.Grid.Rows(j).Cells(1).Value) = TextBox1.Text.ToString() Then
Found = True
Dim newValue As Integer
If Integer.TryParse(Grid.Rows(j).Cells(3).Value, newValue) Then
newValue += 1
Else
newValue = 1
End If
Grid.Rows(j).Cells(3).Value = newValue.ToString()
TextBox1.Clear()
TextBox2.Clear()
Exit For
End If
Next
If Not Found Then
FillDataTable(iRow, TextBox1.Text, TextBox2.Text, 1)
Grid.DataSource = _myTable
TextBox1.Clear()
TextBox2.Clear()
End If
End If
End Sub
I have to assume that if it is not found then the quantity should now be 1, but I don't have the specifications for your project, nor access to all of the objects, such as TextBox1
, TextBox1
, Grid
, nor the FillDataTable
method. If one presses Enter several times, this method would keep incrementing the value by 1.
EDIT: Per Dr. Null's suggestion, changed to ensure data type compatibility.