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

时间:2014-11-13 14:09:28

标签: c# datagridview

我试图在更改之前获取单元格的值。因此,当我进行.Text更改时,我想在执行文本更改之前获取更改前的值。是否会有一个事件可以让我获取这些数据?

我尝试过使用BeginCellEdit,CellValidating等等......但是这些都没有触发,当值更新时,它们甚至不会在方法调用上达到我的断点。

我宁愿在更改过程中不必手动删除大量的get .Texts,只想让一个事件获得预先更改的值。是否有其他方法或方法可以获得预先更改的单元格值?

2 个答案:

答案 0 :(得分:0)

创建一个不需要重复代码的方法怎么样?

而不是叫这个:

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

创建一个方法,在实际更新单元格之前,使用旧值执行所需的操作:

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
}

然后同样称呼它:

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

答案 1 :(得分:-1)

我希望这会对你有所帮助:

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;
}
相关问题