I have a control which extends the DataGridView control. I am overriding the ProcessDialyKey event and throwing and event of my own, which the container form can respond to as the user types in a cell.
The problem I am finding is that when I fire my "CellEditKeyPress" event from within the DataGridView's ProcessDialogKey method, the Value of the cell I am editing has not yet been updated.
So, as the user types 'a', my CellEditKeyPress event fires but when I obtain the value of the cell, that value is still an empty string. User then types 'b', the value I can obtain is 'a', and so on. My event is always effectively one keypress behind.
Here's some code to illustrate:
public class MyDataGridView : DataGridView { public delegate void CellEditKeyPressHandler(Keys keyData, DataGridViewCell currentCell); public event CellEditKeyPressHandler CellEditKeyPress; protected override bool ProcessDialogKey(Keys keyData) { if (CellEditKeyPress != null) { CellEditKeyPress(keyData, this.CurrentCell); } return base.ProcessDialogKey(keyData); } }
...and on my form...after wiring up the CellEditKeyPress (in the designer)
private void myDataGridView1_CellEditKeyPress(Keys keyData, DataGridViewCell currentCell) { myDataGridView1.EndEdit(); if (currentCell.Value != null) { textBox1.Text = currentCell.Value.ToString(); textBox2.Text = currentCell.FormattedValue.ToString(); } myDataGridView1.BeginEdit(false); }
The affect is that the contents of the TextBoxes (1 & 2) is one character behind the contents of the cell I'm editing.
I've tried playing with the order of the ProcessDialogKey method to no avail. (Thinking it may need the base.ProcessDialogKey method called before firing my event, but that didn't change anything.)
I also replaced the "myDataGridView1.EndEdit() with a "this.Validate()" in an attempt to get the control's values up to date, but that made no difference.
Is there a way to ensure I am working with up to date cell content? Am I using the wrong overrides to achieve this?