当我离开当前单元格时尝试获取该单元格的值时,我无法获取该单元格的值。我的代码在事件Cell_Leave
if (e.ColumnIndex == 5)
{
double netVal;
double.TryParse(dgvRegister.Rows[e.RowIndex].Cells[4].Value.ToString(), out netVal);
double Vat;
double.TryParse(dgvRegister.Rows[e.RowIndex].Cells[5].Value.ToString(), out Vat);
string amt = ((netVal / 100) * Vat).ToString();
}
在这种情况下,我得到的是单元格[4]值,当我来到单元格[5].value时,我得到了空值。为什么我得到一个空值?请帮我找到解决方案。
尝试将Value
更改为EditedFormattedValue
dgvRegister.Rows[e.RowIndex].Cells[4].EditedFormattedValue.ToString()
根据spajce的回答,除了dgvRegister.Rows[e.RowIndex].Cells[4]
,你也可以直接使用DataGridView
的属性CurrentCell
。少一点代码。
dataGridView1.CurrentCell.EditedFormattedValue.ToString();
顺便说一句,感谢Spajce向我介绍EditedFormattedValue
。我不知道。