我有一个在 Visual Studio 2005 上用 VS.NET Visual Basic 编写的应用程序。
它有一个 Windows 窗体,上面有一个DataGridView
控件来显示从数据库中检索到的数据。
我希望允许用户右键单击记录,查看上下文菜单并能够单击删除以从数据库中删除该记录。
不幸的是,我什至无法按下鼠标或单击事件来触发(在调试模式下,设置了断点)
这是我的代码:
Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
MessageBox.Show("You right-clicked on the grid at", "Unpaid", MessageBoxButtons.OK)
End Sub
Private Sub DataGridView1_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDoubleClick
MessageBox.Show("You right-clicked on the grid at", "Unpaid", MessageBoxButtons.OK)
End Sub
Private Sub DataGridView1_CellMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDown
If e.Button = Windows.Forms.MouseButtons.Right Then
MessageBox.Show("Clicked right")
else
MessageBox.Show("Clicked left")
End If
Dim r As Rectangle
if (e.RowIndex >= 0 && e.ColumnIndex >= 0 && e.Button == MouseButtons.Right)
DataGridView1.Rows(e.RowIndex).Selected = True
r = DataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, True)
contextMnu.Show(DataGridView1, r.Left + e.X, r.Top + e.Y)
End If
End Sub
Private Sub DeleteChequeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteChequeToolStripMenuItem.Click
MessageBox.Show("You right-clicked on the grid", "Unpaid", MessageBoxButtons.OK)
End Sub
如您所见,我正在尝试获取一些事件,任何要触发的事件,当我右键单击甚至左键单击时,没有任何代码正在运行。
顺便说一句,运行时网格中有数据,据我所知,它已启用
那么我在这里做错了什么?我需要向DataGridView
控件添加一些功能吗?