Compare old and new value in DataGridView cell
Asked Answered
A

6

16

How to change DataGridView cell ForeColor based on whether new cell value is > or < than current/old cell value? Is there an event which passes the new value before the current is changed, so I can compare them?

The data is updated from underlying source, and may be bound by BindingSource.

Artima answered 14/8, 2011 at 22:29 Comment(0)
P
27

I ran into a similar issue. I tackled this by using the CellValidating event instead:

void dgv_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
    var oldValue = dgv[e.ColumnIndex, e.RowIndex].Value;
    var newValue = e.FormattedValue;
}

Admittedly, I just needed access to the old value, I didn't need to perform any formatting. I'm sure you can apply formatting through this event handler, though.

Paviour answered 8/1, 2012 at 6:56 Comment(0)
J
0

If the inner source of DataGridView control is a DataTable then you can utilize the older version of DataRow using DataRowVersion enum. Note that I have utilized CellFormatting Event.

Example:

private void DataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    // if NOT the DataGridView's new row
    if (!this.dataGridView1.Rows[e.RowIndex].IsNewRow)
    {
        // if my desired column
        if (e.ColumnIndex == 0)
        {
            TestDataSet.TestRow row;

            row = (TestDataSet.TestRow)((DataRowView)this.dataGridView1.Rows[e.RowIndex].DataBoundItem).Row;

            if (row.Column1, (int)row["Column1", DataRowVersion.Original]) > 0)
                    e.CellStyle.ForeColor = Color.Red;
        }
    }
}
Joellenjoelly answered 15/8, 2011 at 5:45 Comment(0)
O
0

You may want to look at the DataGridView.CellValueChangedevent (http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvaluechanged.aspx).

If you want to check the value before it is saved, then look at DataGridView.CurrentCellDirtyStateChanged (http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.currentcelldirtystatechanged.aspx).

Orit answered 22/8, 2011 at 8:43 Comment(0)
B
0
var cell = ((DataGridView)sender).Rows[e.RowIndex].Cells[e.ColumnIndex];

    if (cell.Value != e.FormattedValue)
    {
       // Current cell value: cell.Value
       // New cell value: e.FormattedValue
       // ...
    }
Boltzmann answered 9/2, 2024 at 8:48 Comment(1)
Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation?Firenew
S
-1

You can store the old value of the cell in a variable, to compare and change the ForeColor depending on the result, then delete the old value from the variable.

Regards.

Suppurate answered 14/8, 2011 at 22:44 Comment(1)
I was hoping to avoid that, as I need for many columns, but if it's no easier way...Artima
S
-1
    private double CuerrLineQty;
    private void GridTransaction_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
    {
        if (currOp != CurrentOperation.FillDone)
            return;

        CuerrLineQty = double.Parse(GridTransaction.Rows[e.RowIndex].Cells["DisplayQty"].Value.ToString());
        
        CuerrLineQty = double.Parse(GridTransaction.Rows[e.RowIndex].Cells["DisplayQty"].FormattedValue.ToString());

        CuerrLineQty = double.Parse(GridTransaction.Rows[e.RowIndex].Cells["DisplayQty"].EditedFormattedValue.ToString());

    }

screenshot

Shermanshermie answered 6/11, 2021 at 1:31 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.