C# DataGridViewCell.FormattedValue是如何更新的?

C# DataGridViewCell.FormattedValue是如何更新的?,c#,winforms,datagridview,C#,Winforms,Datagridview,我有一个扩展DataGridView控件的控件。我正在重写我自己的ProcessDialyKey事件和抛出and事件,当用户在单元格中键入时,容器窗体可以响应该事件 我发现的问题是,当我从DataGridView的ProcessDialogKey方法中触发“CellEditKeyPress”事件时,我正在编辑的单元格的值尚未更新 因此,当用户键入“a”时,我的CellEditKeyPress事件将触发,但当我获取单元格的值时,该值仍然是一个空字符串。然后用户输入'b',我可以得到的值是'a',依

我有一个扩展DataGridView控件的控件。我正在重写我自己的ProcessDialyKey事件和抛出and事件,当用户在单元格中键入时,容器窗体可以响应该事件

我发现的问题是,当我从DataGridView的ProcessDialogKey方法中触发“CellEditKeyPress”事件时,我正在编辑的单元格的值尚未更新

因此,当用户键入“a”时,我的CellEditKeyPress事件将触发,但当我获取单元格的值时,该值仍然是一个空字符串。然后用户输入'b',我可以得到的值是'a',依此类推。我的活动实际上总是落后一键

下面是一些代码来说明:

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); } } 公共类MyDataGridView:DataGridView { 公共委托void CellEditKeyPressHandler(Keys keyData、DataGridViewCell currentCell); 公共事件CellEditKeyPress处理器CellEditKeyPress; 受保护的覆盖布尔ProcessDialogKey(Keys keyData) { 如果(CellEditKeyPress!=null) { CellEditKeyPress(keyData,this.CurrentCell); } 返回base.ProcessDialogKey(keyData); } } …在我的表格上…在连接CellEditKeyPress后(在设计器中)

私有void myDataGridView1_CellEditKeyPress(键键数据,DataGridViewCell currentCell) { myDataGridView1.EndEdit(); if(currentCell.Value!=null) { textBox1.Text=currentCell.Value.ToString(); textBox2.Text=currentCell.FormattedValue.ToString(); } myDataGridView1.BeginEdit(false); } 其影响是文本框(1和2)的内容在我正在编辑的单元格内容后面一个字符

我尝试过使用ProcessDialogKey方法的顺序,但没有效果。(认为可能需要在触发我的事件之前调用base.ProcessDialogKey方法,但这并没有改变任何事情。)

我还用“this.Validate()”替换了“myDataGridView1.EndEdit()”,试图使控件的值更新,但这没有什么区别

有没有办法确保我使用的是最新的单元格内容?我是否使用了错误的覆盖来实现这一点?

这对我很有用:

    private void myDataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
    {
        myDataGridView1.EndEdit();

        if (myDataGridView1.CurrentCell.Value != null)
        {
            textBox1.Text = myDataGridView1.CurrentCell.Value.ToString();
            textBox2.Text = myDataGridView1.CurrentCell.FormattedValue.ToString();
        }

        myDataGridView1.BeginEdit(false);

    }
这里的帖子会帮你解决这个问题。在你更改CurrentCell之前,不会调用CellValidating。因此,我在这个问题上做了很多努力,就是更改CurrentCell,然后切换回当前的Cell

protected override bool ProcessDialogKey(Keys keyData)
{
    DataGridViewCell currentCell = CurrentCell;
    EndEdit();
    CurrentCell = null;
    CurrentCell = currentCell;
    if (CellEditKeyPress != null)
    {
        CellEditKeyPress(keyData, this.CurrentCell);
    }
    return base.ProcessDialogKey(keyData);
}

您需要为活动编辑器处理
KeyUp
事件。但是,我不确定如何获取它。是的-DataGridView编辑控件的KeyUp事件可能会发生?但是Jacob的回答表明我只是使用了错误的事件。谢谢。EditingControl可以更改;每当事件更改时,您都必须挂起并取消挂起事件。谢谢t、 困难在于,我想对每次按键做出反应,而不仅仅是输入键,所以每次按键时切换当前单元格有点混乱。
protected override bool ProcessDialogKey(Keys keyData)
{
    DataGridViewCell currentCell = CurrentCell;
    EndEdit();
    CurrentCell = null;
    CurrentCell = currentCell;
    if (CellEditKeyPress != null)
    {
        CellEditKeyPress(keyData, this.CurrentCell);
    }
    return base.ProcessDialogKey(keyData);
}