为什么在将文本框添加到vb.net中的datagridview时,数量列不会递增?

huangapple go评论68阅读模式
英文:

Why doesn't qty column increment when adding in textbox to datagridview in vb.net

问题

以下是已翻译的代码部分:

 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

希望这有助于您的问题。如果需要更多帮助,请随时提出。

英文:

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

为什么在将文本框添加到vb.net中的datagridview时,数量列不会递增?

Desired result

No CodeProduct Barcode Qty
1 1000 1000 3
2 1002 1002 1

答案1

得分: 2

代码中的问题是您将单元格的值设置为2,而不是递增它。您设置了这个:

Dim Qty As Integer = 1

然后执行了这个操作:

Grid.Rows(j).Cells(3).Value = Qty + 1

所以如果您实际上想要将单元格的值增加1,那么您应该改用这个:

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()

而且不需要定义 Qty 变量。

另外,您可能希望首先检查是否按下了 Enter 键,因为似乎您不希望在此方法中处理其他按键。这是修改后的代码:

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

我假设如果找不到,那么数量现在应该为1,但我没有您项目的详细规格,也没有访问所有对象,如 TextBox1TextBox2Grid 以及 FillDataTable 方法。如果按下 Enter 键多次,这个方法会持续递增值。

英文:

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.

答案2

得分: 0

感谢 @jmcilhinney 的指南。

Grid.Rows(j).Cells(3).Value += Qty
英文:

thanks for the guide @jmcilhinney

Grid.Rows(j).Cells(3).Value += Qty

huangapple
  • 本文由 发表于 2023年7月6日 11:58:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76625403.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定