keep track of object changes in datagridview using the entity framework
I'm using a DataGridView to display EntityObjects from the .NET 开发者_如何转开发Entity Framework.
how could I change the formatting of a row of the DataGridView if the corresponding EntityObject has been altered by the user, e.g. displaying the row in bold
greetings
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;
}
}
精彩评论