WPF-DataGrid
实现目标:
绑定数据源后,根据实体的最值,更改单元格颜色。
类似于winform中datagridview 的cellFormatter事件
Winform DataGridView
private void datagridview1_CellFormatting(object sender ,DataGridViewCellFormattingEventArgs e)
{
if(dataGridView1.Columns[e.ColumnIndex].DataPropertyName == “StrValue”)
{
double dval = Convert.ToDouble(e.Value);
var entity = dataGridView1.Rows[e.RowIndex].DataBoundItem as Entity;//绑定的实体类
//判断大小,设置颜色
if(dval > entity.Max || dval < entity.Min)
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Red;
else
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Black;
}