C# datagridview CellLeave

来源:百度知道 编辑:UC知道 时间:2024/06/25 05:25:16
如果不是空,则判断是否为数字型,再判断是否其对应存货编码是否写了
*/
if (e.ColumnIndex == 3)
{
string aa;
aa = dataGridView2.CurrentCell.EditedFormattedValue.ToString();

dataGridView2.CurrentCell.Value = aa;
// try
// {
if (dataGridView2.CurrentCell.Value != null)
{
try
{
decimal i = decimal.Parse(dataGridView2.CurrentCell.Value.ToString());

}
catch
{
MessageBox.Show("分子必须是数字");
dataGridView2.CurrentCell.Value = null;
return;
}

//判断存货编码是否已输入
try
{
str

换个事件吧. CellEndEdit

因为,dataGridView有个属性IsCurrentCellDirty(获取一个值,该值指示当前单元格是否有未提交的更改。)我们来看看为什么会出现你的那种情况.
当你编辑单元格离开的时候,IsCurrentCellDirty是TRUE,所以,你判断a = dataGridView2[1, e.RowIndex].Value.ToString(); 这个是没用的,dataGridView1.CurrentCell.Value中还是初值,你刚输入的值还没有反映上去.通过这个IsCurrentCellDirty属性,我们可知dataGridView没有把刚更新的值写入VALUE,他把值暂时记录在了EditedFormattedValue中,所以此时VALUE!=EditedFormattedValue.

但当你换个事件处理CellEndEdit,这个发生在输入后,dataGridView把你输入的值提交给了VALUE了,这时候,你可以做个试验,IsCurrentCellDirty 为FALSE,VALUE==EditedFormattedValue.

回答楼主:
CellValueChanged先执行,再执行CellEndEdit
楼主应该做做试验,把合计,验证等工作安排好.那个先那个迟,你应该比我清楚.反正,你开始用的CellLeave肯定是没法用的了,因为他根本没法真正确定你输入时的值的状态.

代码问题。
之所以报错是因为this.dataGridView2.Rows[e.RowIndex].Cells[0].Value肯能为NULL,此时再TOString()的时候就报错了。
所以之前需要判断是否为NULL。
至于修改前要完成修改状态。
this.dataGridView2.EndEdit()