简体   繁体   English

DataGridView获取预先更改的单元格值

[英]DataGridView get pre-changed cell value

I am trying to get the value of a cell right before it changes. 我试图在更改之前立即获取单元格的值。 So when I do a .Text change, I want to grab the pre-change value before doing the text change. 因此,当我进行.Text更改时,我想在进行文本更改之前获取更改前的值。 Is there an event that will fire allowing me to get this data? 是否有一个事件可以触发我获取此数据?

I have tried using BeginCellEdit, CellValidating, ect... but none of these are firing, they don't even hit my break points on the method call when a value is updated. 我尝试使用BeginCellEdit,CellValidating等...,但是这些都没有触发,当值更新时,它们甚至都没有在方法调用上达到我的断点。

I would rather not have to manually drop a lot of get .Texts during changes, and just want one event to get pre-changed values. 我宁愿不必在更改期间手动删除很多get .Texts ,而只希望一个事件获得预先更改的值。 Are there any other methods or ways to get a pre-changed cell value? 是否有其他方法或方法来获取预先更改的单元格值?

How about creating a method that eliminates the need for duplicate code? 如何创建一种无需重复代码的方法?

Instead of calling this: 而不是这样:

frameGridView.Rows[frameNumber].Cells[1].Value = subArray[0];

Create a method that does what you need with the old value, before it actually updates the cell: 在实际更新单元格之前,创建一个使用旧值执行所需操作的方法:

private void UpdateGrid(int rowIndex, int columnIndex, string newValue)
{
    var cell = frameGridView.Rows[frameNumber].Cells[1];

    var oldValue = Convert.ToString(cell.Value);
    // display oldValue in the section that shows recent changes

    cell.Value = newValue;  // update the cell
}

Then call it likewise: 然后同样地称呼它:

UpdateGrid(frameNumber, 1, subArray[0]);

I hope that will help you: 希望对您有所帮助:

private string oldValue;
private bool isValid;

void myDataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
 this.isValid= true;
 this.oldValue= Convert.ToString(this[e.ColumnIndex, e.RowIndex].Value);

 if (!isValid(this.myDataGridView(e))
 isValid= false;
}

void myDataGridView_CellValidated(object sender, DataGridViewCellValidatingEventArgs e)
{
 if (!isValid)
 this[e.ColumnIndex, e.RowIndex].Value = this.oldValue;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM