Here’s what I’m trying to do in my 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);
}
}
}
Yet, the value isn’t changed of the cell I leave.
If I type 258 in the cell and leave, it should have 25.
Any suggestions?
You may need to cause validation to occur for it to take place. At the end, add
this.Validate();You might also want to try using the CellEndEdit event instead, since this even is fired after the cell leave and validation events.