2011-05-16 48 views
0

这里就是我想在我的WinForms DataGridView中要做到:为什么我的单元格值没有被截断?

private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e) 
{ 
    if (e.ColumnIndex > 1) 
    { 
     string tmpValue = Convert.ToString(dataGridView1[e.ColumnIndex, e.RowIndex].Value); 

     if (tmpValue.Length >= 3) 
     { 
      //At thsi point, I'm 100% sure that tmpValue has "253" as it's value. 
      dataGridView1[e.ColumnIndex, e.RowIndex].Value = tmpValue.Substring(0, 2); 
     } 
    } 
} 

然而,该值没有改变细胞我离开的。

如果我输入的单元258和离开,它应该有25

有什么建议?

回答

1

您可能需要导致出现验证它发生。最后,添加this.Validate();

您可能还想尝试使用CellEndEdit事件,因为它甚至会在单元格离开和验证事件后触发。

+0

该方法不存在。 – 2011-05-16 12:36:25

+0

我刚刚检查过,我必须离开牢房,回到它,然后再次离开**然后**按预期工作。有任何想法吗? – 2011-05-16 12:41:09

+0

也许他们是指'无效'? – 2011-05-16 12:41:11

0

也许是:dataGridView1.DataBind()

相关问题