You can retrieve the state of an object using the ObjectStateManager
:
public EntityState GetState(object o)
{
var entry = context.ObjectStateManager.GetObjectStateEntry(o);
return entry.State;
}
You can handle the CellPainting
event of the DataGridView
to change the style of the row according to the entity state
private grid_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
object entity = grid.Rows[e.RowIndex].DataBoundItem;
var state = GetState(o);
switch(state)
{
case Detached :
e.CellStyle.Font = italicFont;
break;
case Unchanged :
e.CellStyle.Font = normalFont;
break;
case Added :
e.CellStyle.Font = boldFont;
break;
case Deleted :
e.CellStyle.ForeColor = Color.Red;
break;
case Modified :
e.CellStyle.Font = boldFont;
break;
}
}