【问题标题】:How to open form when a specific cell in DataGridView Single-click in vb.net?在vb.net中单击DataGridView中的特定单元格时如何打开表单?
【发布时间】:2021-10-03 12:46:26
【问题描述】:
+-------+-------+-------+-------+
|   a   |   b   |   c   |   d   |
+-------+-------+-------+-------+
|   1.  |   b1  |   c1  |   d1  |
+-------+-------+-------+-------+

问题是如何制作一个单元格ex:b1单击并显示一个表单?但是当 c1 或 d1 点击表单时不会打开,唯一的光标在 c1 或 d1 的地方。

我已经使用了 CellClick 事件,但是当我点击时所有的单元格都会打开表单,这不是我想要的。我使用 VB.NET。

【问题讨论】:

  • 测试 哪个实际单元格 在 cellclick 中被点击 - VB 不是读心器,并且无法知道只在点击 B1 而不是 C1 时引发点击事件 - 它每次点击都会引发,然后您必须对其进行编程以决定是否采取行动
  • 如果我点击 c1 然后打开表单...!如何打开?

标签: vb.net


【解决方案1】:

只需使用 _CellMouseUp 事件,例如:

Private Sub DataGridView1_CellMouseUp(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseUp
    Dim i As Integer
    Dim j As Integer
    i = DataGridView1.CurrentCell.RowIndex
    j = DataGridView1.CurrentCell.ColumnIndex
    If i = 5 And j = 2 Then
        MsgBox(i & "  " & j)
    End If
End Sub

【讨论】:

  • 表单打开代码在哪里...?我想点击单元格然后显示表单如何做请帮助我
  • @Aryan 博士---获取单元格的坐标,i 和 j,你想打开表单并将 msgbox 行替换为 yourformName.show
  • 是的,它的工作比很多人都好
  • 我对你的用户名和发布的代码之间的二分法感到最有趣
【解决方案2】:

使用DataGridViewCellEventArgs。它们以e 的形式传递给点击事件。第一行数据(不包括标题)的索引为 0。列也从 0 开始索引。因此,在您的图表中,列 c 将是索引 2。您会发现 .net 索引从 0 开始。

Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
    If e.RowIndex = 0 AndAlso e.ColumnIndex = 2 Then
        Form2.Show()
    End If
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-08
    • 2016-07-18
    相关资源
    最近更新 更多