Changing background of Added rows

Nov 28, 2008 at 2:43 PM
Hi, When rows are added to my datagrid or underlying datatable I want to indicate they are new by changing the background colour. Currently I have this working by setting the cell background individually, however as soon as the grid is sorted the background colours are lost.

My grid has auto generating columns on and the datatable defines the columns. Ideally I'd like a DataTrigger on the row which looks at the underlying RowState and changes colour if its Added? But I can't work out how to do it.

Any help most appreciated.

Dec 1, 2008 at 9:14 AM

This one has been bugging me for a while!

My initial thought was - that's easy, the Item associated with each row will be a DataRowView, from here you can navigate to the RowState .... DataRowView.Row.RowState, giving the following style:

<Style x:Key="MyCellStyle" TargetType="{x:Type dg:DataGridCell}">
        <DataTrigger Binding="{Binding Path=Row.RowState, Mode=OneWay}" Value="Modified">
            <Setter Property="Background" Value="Yellow"/>

The problem is changes to DataRow.RowState do not result in any property change notification, therefore when the row state changes the above trigger is not re-evaluates. Teh only solution I could come up with was to 'refresh' the above style by removing it then re-applying to the grid when the current items selection is changed:

<dg:DataGrid ItemsSource="{Binding}" CellStyle="{StaticResource MyCellStyle}" SelectionChanged="DataGrid_SelectionChanged" />

private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    DataGrid grid = sender as DataGrid;

    Style style = grid.CellStyle;
    grid.CellStyle = null;
    grid.CellStyle = style;


This has been bugging me for a while - but I haven't come up with a better solution yet, other than of course managing this entirely programmatically by styling each row as the Selection changes.

Colin E.
-- - my WPF blog - WPF DataGrid Practical Examples

Feb 23, 2009 at 2:47 PM
I have another ugly solution to this problem:

1) Listen to RowChanged on the DataTable

_dataTable.RowChanged += DataTableOnRowChanged;

2) In the listener, find the UI element that corresponds to the changed row and explicitly update the binding (I'm using the fontweight on a textblock - adjust accordingly):
        private void DataTableOnRowChanged(object sender, DataRowChangeEventArgs args) {
            var n = DataGrid.Items.Count;

            for(var i = 0; i < n; ++i)
                var x = (DataGridRow)DataGrid.ItemContainerGenerator.ContainerFromIndex(i);
                if(x != null && ((DataRowView)x.Item).Row == args.Row) {

        private static void UpdateBinding(DependencyObject ui) {
            var iend = VisualTreeHelper.GetChildrenCount(ui);
            for(var i = 0; i < iend; ++i) {
                var e = (UIElement) VisualTreeHelper.GetChild(ui, i);

                if(e is TextBlock) {
                    var expr = BindingOperations.GetBindingExpression(e, TextBlock.FontWeightProperty);
                    if(expr != null) {