使用ContextMenuStip调用DataGridView用户删除行

时间:2013-04-23 11:34:12

标签: vb.net datagridview contextmenustrip

我使用[dgv_clients_UserDeletingRow]调用事件[ContextMenuStrip]时遇到问题。

它给了我这个错误:

Unable to cast object of type 'System.EventArgs' to type 'System.Windows.Forms.DataGridViewRowCanelEventArgs'

这是我的代码:

  Private Sub dgv_clients_UserDeletingRow(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowCancelEventArgs) Handles dgv_clients.UserDeletingRow
    deleteLine(e)
End Sub

  Private Sub toolStripItem1_Click(ByVal sender As Object, ByVal args As EventArgs) Handles toolStripItem1.Click

    If mouseLocation.RowIndex > 0 Then
        dgv_clients.Rows(mouseLocation.RowIndex).Selected = True
        dgv_clients.Rows(mouseLocation.RowIndex).DefaultCellStyle.SelectionBackColor = Color.Gray
        DeleteToolStripMenuItem_Click(sender, args)
    End If

End Sub
Public Sub DeleteToolStripMenuItem_Click(ByVal Sender As Object, ByVal e As EventArgs)
    Call deleteLine(e)
End Sub

谢谢大家

1 个答案:

答案 0 :(得分:1)

问题是您的deleteLine方法。

您使用两种不同的类型调用它。在Sub dgv_clients_UserDeletingRow传递e as DataGridViewRowCancelEventArgSub DeleteToolStripMenuItem_Click传递e as EventArgs。如果你没有为这两种类型重载deleteLine方法,编译器将尝试自动解析错误的类型并且失败。

相关问题