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