Compare old and new value in DataGridView cell

前端 未结 4 746
暗喜
暗喜 2021-01-11 11:18

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

4条回答
  •  醉梦人生
    2021-01-11 11:58

    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;
            }
        }
    }
    

提交回复
热议问题