【问题标题】:select row content on datagridview by hitting "enter" key in vb.net ,通过点击 vb.net 中的“enter”键选择 datagridview 上的行内容,
【发布时间】:2015-06-16 09:49:12
【问题描述】:

我为此创建了一个 datagridview 和一些文本框 我想如果一个人在选定的 ro 中输入相应的值,则应在表单的文本框中填写相应的值。

我当前的代码提供给我,但它在 cellclick 事件中,但我在按下 enter 键事件中需要它。

Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
    If e.RowIndex >= 0 Then
        Dim row As DataGridViewRow
        row = Me.DataGridView1.Rows(e.RowIndex)
        iname.Text = row.Cells("Name").Value.ToString
        icode.Text = row.Cells("Item code").Value.ToString
        irate.Text = row.Cells("Rate").Value.ToString
        icomnt.Text = row.Cells("Comment").Value.ToString

    End If
End Sub

【问题讨论】:

  • DGV 确实有 KeyDown、KeyUp 和 KeyPress 事件。你试过这些吗?
  • 我试过但没有得到解决方案。
  • 显示代码怎么样,也许有什么不对的地方我们可以看看。
  • If e.KeyCode = Keys.Enter And DataGridView1.CurrentCell.RowIndex >= 0 Then e.Handled = True e.SuppressKeyPress = True Dim row As DataGridViewRow row = Me.DataGridView1.Rows(DataGridView1. CurrentCell.RowIndex) iname.Text = row.Cells("Name").Value.ToString
  • 不清楚您的代码有什么问题。通过单击标签的编辑链接,将您评论中的代码放入您的问题中。

标签: vb.net datagridview enter


【解决方案1】:

Private Sub DataGridView1_KeyPress(sender As Object, e As KeyPressEventArgs) 处理 DataGridView1.KeyPress

    If e.KeyChar = ChrW(Keys.Enter) Then
        DataGridView1.CurrentCell = DataGridView1.Rows(DataGridView1.CurrentRow.Index - 1).Cells(0)
        txtno.Text = DataGridView1.SelectedCells(0).Value
        
    End If
   
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-21
    • 2011-12-30
    • 1970-01-01
    • 1970-01-01
    • 2013-01-22
    • 1970-01-01
    • 2013-07-03
    相关资源
    最近更新 更多