I have a Datagridview. I'm trying to get the old value (value before edit) and the new value, then compare.
private void GridDetails_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (e.ColumnIndex == GridDetails.Columns["Address"].Index)
{
var oldValue = GridDetails[e.ColumnIndex, e.RowIndex].Value;
var newValue = GridDetails[e.ColumnIndex, e.RowIndex].EditedFormattedValue;
if(oldValue!=newValue)
{
GridDetails.Rows[e.RowIndex].Cells["Address"].Style.BackColor = Color.FromArgb(212, 212, 155);
}
}
}
But Both old and new values are the same.
您可以使用 DataGrid 的 CellBeginEdit 事件并将值存储在某处然后使用 CellEndEdit 将新值与旧值进行比较。
Chamod, you are right. I used CellBeginEdit and CellEndEdit, and it worked. Below is my answer.
string oldValue;
private void GridDetails_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
oldValue = GridDetails[e.ColumnIndex, e.RowIndex].Value.ToString();
}
private void GridDetails_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (e.ColumnIndex == GridDetails.Columns["Address"].Index)
{
string newValue = e.FormattedValue.ToString();
if(oldValue!=newValue)
{
GridDetails.Rows[e.RowIndex].Cells["Address"].Style.BackColor = Color.FromArgb(212, 212, 155);
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.