C# Windows 表单 - 如何获取单元格的 previous/old 值
C# Windows Form - How to get previous/old value of cell
我有一个 Datagridview。我正在尝试获取旧值(编辑前的值)和新值,然后进行比较。
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);
}
}
}
但是新旧值是一样的
您可以使用 DataGrid 的 CellBeginEdit 事件并将值存储在某处然后使用 CellEndEdit 将新值与旧值进行比较。
Chamod,你是对的。我使用了 CellBeginEdit 和 CellEndEdit,它起作用了。以下是我的回答。
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);
}
}
}
我有一个 Datagridview。我正在尝试获取旧值(编辑前的值)和新值,然后进行比较。
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);
}
}
}
但是新旧值是一样的
您可以使用 DataGrid 的 CellBeginEdit 事件并将值存储在某处然后使用 CellEndEdit 将新值与旧值进行比较。
Chamod,你是对的。我使用了 CellBeginEdit 和 CellEndEdit,它起作用了。以下是我的回答。
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);
}
}
}