[英]Invalid Cast Exception was unhandled
我想嘗試在函數內調用事件,而我想調用的事件是DataGridViewCellEventArgs
,但它在函數內。 但是,當我嘗試下面的代碼時,出現了此錯誤: Unable to cast object of type 'System.Windows.Forms.MouseEventArgs' to type 'System.Windows.Forms.DataGridViewCellEventArgs'.
這是代碼:
private void UpdateQuantityDataGridView(object sender, EventArgs e)
{
(...Other codes)
var _productCode = dataGridView1["Product Code", ((DataGridViewCellEventArgs)e).RowIndex].Value;
(...Other codes)
}
當用戶完成在DataGridView
的數據編輯並按“確定”按鈕時,我調用上面的函數,上面的函數是更新用戶對數據庫以及DataGridView中所做的任何更改
您正在嘗試將EventArgs(parent type)
為DataGridViewCellEventArgs(child type)
。 在這種情況下,這是不安全的。 您可以創建一個新的DataGridViewCellEventArgs
。
var _productCode = dataGridView1["Product Code",(new DataGridViewCellEventArgs(columnIndex, rowIndex)).RowIndex].Value;
在這里創建一個新的DataGridViewCellEventArgs
。 由於它是與單元格相關的操作的事件參數,因此需要columnIndex
和rowIndex
來定位該單元格。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.