c# – 如何从Cell_Leave事件中获取DataGridViewCell的值?

前端之家收集整理的这篇文章主要介绍了c# – 如何从Cell_Leave事件中获取DataGridViewCell的值?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. private void dataGridView1_CellLeave(object sender,DataGridViewCellEventArgs e)
  2. {
  3. if (e.ColumnIndex > 1)
  4. {
  5. int cellValue = Convert.ToInt32(((DataGridViewCell)sender).Value);
  6.  
  7. if (cellValue < 20)
  8. {
  9. ((DataGridViewCell)sender).Value = 21;
  10. }
  11. }
  12. }

我正在尝试获取事件触发的单元格的值.

当我尝试将发送者强制转换为DataGridViewCell时会触发异常:

Unable to cast object of type
‘System.Windows.Forms.DataGridView’ to
type
‘System.Windows.Forms.DataGridViewCell’.

你推荐我做什么?

我需要检查值是否小于20,如果是,则将其提高到21.

解决方法

尝试使用DataGrid [e.RowIndex,e.ColumnIndex] .Value.我希望发送者更可能是DataGridView对象而不是单元本身.

猜你在找的C#相关文章