views:

352

answers:

1

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

+1  A: 

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;
    }
}
Thomas Levesque
nice solution thank you
pragmascript