2013-04-23 61 views
0

我正面臨使用[ContextMenuStrip]調用事件[dgv_clients_UserDeletingRow]的問題。使用ContextMenuStip調用DataGridView用戶刪除行

它給我的錯誤:

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

問題是你deleteLine方法。

你用兩種不同的類型調用它。在Sub dgv_clients_UserDeletingRow中,您通過e as DataGridViewRowCancelEventArg s,在Sub DeleteToolStripMenuItem_Click中您通過e as EventArgs。如果您沒有爲這兩種類型重載您的deleteLine方法,編譯器將嘗試自動分析錯誤的類型,並且失敗。

+0

感謝您的幫助,但我用另一種方法解決了問題。 – vertebre 2013-04-24 13:05:06