How can I retrieve the previous value of a DataGridView cell using the CellValueChanged event?

怎甘沉沦 提交于 2019-12-01 06:10:29

You could try another aproach like this:

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    newvalue = (int)dataGridView1[e.ColumnIndex, e.RowIndex].Value;
}

private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
    oldvalue = (int)dataGridView1[e.ColumnIndex, e.RowIndex].Value;
}

Assuming its an int,if its another datatype then will also work(except offcourse the variables oldvalue and newvalue must be that type also).

Or by your question,its just about the old value,then you will only need the CellBeginEdit event and then use the oldvalue variable inside the validating event.

IMHO better solution, cause underlying DataTable will not be marked as changed if you reject the value. Old value will be displayed automatically

private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    var underlyingDataRow = ((DataRowView)dataGridView.Rows[e.RowIndex].DataBoundItem).Row;
    if (DoesNotMeetYourCondition)
    {
       row.RejectChanges();
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!