You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET中TextBox添加数据时DataGridView的Qty列无法递增问题

VB.NET中DataGridView的Qty列无法持续递增问题排查

问题描述

在VB.NET开发中,通过TextBox向DataGridView添加数据时,Qty列无法实现持续递增,当前最大值仅能达到2。当输入的产品码与DataGridView中已存在的产品码相同时,期望Qty值可持续累加,请求排查代码问题。

当前效果

重复输入同一产品码时,Qty列最多只能增加到2,无法继续累加。

期望结果

NoCodeProductBarcodeQty
1100010003
2100210021

相关代码

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
                ' 问题出在以下代码行
                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

问题排查与修复

问题根源

  1. 数据源与UI不同步:直接修改DataGridView单元格值,未更新底层数据源_myTable,后续操作读取的仍是数据源中的旧值,导致无法持续累加。
  2. 累加逻辑错误:固定使用Qty + 1(Qty始终为1),未读取当前行实际的Qty值进行累加,所以每次最多只能到2。
  3. 参数不匹配:调用FillDataTable时仅传入4个参数,但方法定义需要6个参数,运行时会触发参数数量不匹配错误。

修复后的代码

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
        ' 遍历数据源_myTable而非DataGridView,保证数据一致性
        For Each row As DataRow In _myTable.Rows
            If row("Codeproduct").ToString() = TextBox1.Text.ToString() Then
                Found = True
                ' 读取当前Qty值并累加1
                row("Qty") = Convert.ToInt32(row("Qty")) + 1
                ' 重新绑定数据源刷新UI
                Grid.DataSource = Nothing
                Grid.DataSource = _myTable
                TextBox1.Clear()
                TextBox2.Clear()
                Exit For
            End If
        Next
        If Not Found Then
            ' 补充缺失的Coledit和ColDel参数(根据实际业务传值,这里示例传空字符串)
            FillDataTable(iRow, TextBox1.Text, TextBox2.Text, Qty, "", "")
            Grid.DataSource = _myTable
            TextBox1.Clear()
            TextBox2.Clear()
        End If
    End If
End Sub

修复说明

  • 直接操作数据源_myTable的行数据,确保数据源与UI展示一致;修改后重新绑定数据源刷新DataGridView。
  • 读取当前行的Qty值,转换为整数后再加1,实现持续累加效果。
  • 补充FillDataTable方法所需的缺失参数,避免运行时错误。

内容的提问来源于stack exchange,提问作者roy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 08:44:59