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

VB.NET中DataGridView间传值失败及快捷键调用窗体问题

VB.NET数据传递失败排查与键盘快捷键替代按钮方案

运行结果

一、数据传递失败的问题修复

你的代码存在三个关键问题导致主窗体无数据显示:

1. UnitPrice属性赋值错误

在frmSearchdt的UnitPrice属性Set方法中,错误地将值赋给了_item而非_unitPrice,导致单价数据无法正确传递:

Public Property UnitPrice As String
    Get
        Return _unitPrice
    End Get
    Set(ByVal value As String)
        ' 原错误代码:_item = value
        _unitPrice = value ' 修正为正确赋值
    End Set
End Property

2. 未设置对话框返回结果

SelectedRow方法中仅调用Me.Hide(),未设置DialogResult = DialogResult.OK,导致主窗体无法识别“确认选择”的操作,不会执行数据赋值逻辑:

Sub SelectedRow(ByVal index As Integer)
    ' 新增:设置对话框返回结果为OK
    Me.DialogResult = DialogResult.OK
    
    Me.Hide()

    Dim rowIndexMainGrid = CInt(txtRowIndex.Text)
    _rowIndexMainGrid = rowIndexMainGrid

    _item = DataGridView1.Rows(index).Cells(0).Value
    _itemDesc = DataGridView1.Rows(index).Cells(1).Value
    _qty = "0"
    _itemUnit = DataGridView1.Rows(index).Cells(2).Value
    _unitPrice = DataGridView1.Rows(index).Cells(3).Value
End Sub

3. 确保txtRowIndex控件可被外部访问

在frmSearchdt设计界面中,将txtRowIndex控件的Modifiers属性设置为Public,否则主窗体无法给它赋值,导致行索引传递失败。

二、用键盘快捷键替代DataGridViewButtonColumn

可以通过监听主窗体DataGridView1的按键事件,设置快捷键触发搜索窗体,步骤如下:

1. 移除按钮列(可选)

如果不需要原有的SEARCH按钮列,可删除BindGrid中添加按钮列的代码:

' 以下代码可删除
'Dim btn As New DataGridViewButtonColumn
'btn.HeaderText = ""
'btn.Text = "SEARCH"
'btn.Name = "btn"
'btn.DefaultCellStyle.BackColor = Color.Black
'btn.DefaultCellStyle.ForeColor = Color.White
'btn.UseColumnTextForButtonValue = True
'DataGridView1.Columns.Insert(0, btn)

2. 添加快捷键处理事件

在frmMaindt中添加DataGridView1_KeyDown事件处理,以Ctrl+F为例:

Private Sub DataGridView1_KeyDown(sender As Object, e As KeyEventArgs) Handles DataGridView1.KeyDown
    ' 按下Ctrl+F触发搜索窗体
    If e.Control AndAlso e.KeyCode = Keys.F Then
        ' 验证当前选中有效行
        If DataGridView1.CurrentRow IsNot Nothing AndAlso DataGridView1.CurrentRow.Index >= 0 Then
            Dim frmSearchdt As New frmSearchdt()
            frmSearchdt.txtRowIndex.Text = DataGridView1.CurrentRow.Index.ToString()
            frmSearchdt.txtRowIndexSearch.Text = "0"

            Dim result As DialogResult = frmSearchdt.ShowDialog()
            If result = DialogResult.OK Then
                DataGridView1.Rows(frmSearchdt.RowIndexMainGrid).Cells(1).Value = frmSearchdt.ItemCode
                DataGridView1.Rows(frmSearchdt.RowIndexMainGrid).Cells(2).Value = frmSearchdt.ItemDesc
                DataGridView1.Rows(frmSearchdt.RowIndexMainGrid).Cells(3).Value = frmSearchdt.Qty
                DataGridView1.Rows(frmSearchdt.RowIndexMainGrid).Cells(4).Value = frmSearchdt.ItemUnit
                DataGridView1.Rows(frmSearchdt.RowIndexMainGrid).Cells(5).Value = frmSearchdt.UnitPrice
            End If
        End If
        e.Handled = True ' 阻止默认按键行为
    End If
End Sub

3. 优化:限定触发列(可选)

如果希望仅选中特定列(比如Item列)时才响应快捷键,可增加列判断:

If e.Control AndAlso e.KeyCode = Keys.F Then
    If DataGridView1.CurrentRow IsNot Nothing AndAlso DataGridView1.CurrentRow.Index >= 0 Then
        ' 仅当选中第1列(Item列)时触发
        If DataGridView1.CurrentCell.ColumnIndex = 1 Then
            ' 搜索窗体逻辑...
        End If
    End If
End If

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 05:27:01