private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex > 1)
{
int cellValue = Convert.ToInt32(((DataGridViewCell)sender).Value);
if (cellValue < 20)
{
((DataGridViewCell)sender).Value = 21;
}
}
}
我試圖獲取事件觸發的單元格的值。如何從Cell_Leave事件中獲取DataGridViewCell的值?
當我嘗試投sender
到DataGridViewCell
一個例外是觸發:
無法轉換類型 「System.Windows.Forms.DataGridView」的對象 類型 「System.Windows.Forms的.DataGridViewCell」。
你推薦我做什麼?
我需要檢查,如果該值小於20,如果是,撞它高達21
這是一個很好的答案,因爲有時候會創建一個通用的事件處理程序。那是;事件處理程序旨在爲多個DataGridView提供服務。 Dulini Atapattu通過將sender參數作爲DataGridView對象進行查詢來完成這一技巧。 – netfed 2017-11-11 04:30:15