pragmascript
pragmascript

Reputation: 13

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

Upvotes: 1

Views: 1508

Answers (1)

Thomas Levesque
Thomas Levesque

Reputation: 292685

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

Upvotes: 1

Related Questions