Articles in this section
Category / Section

How to change row background based on row state when underlying source is data table in WPF DataGrid (SfDataGrid)?

2 mins read

You can change the row background based on RowState.Modified when binding WPF DataGrid (SfDataGrid) to DataTable using style for initial loading. You can also change the background for runtime changes using the DataTable.RowChanged event.

In the following code example, VirtualizingCellsControl’s background is changed based on the DataRow.RowState property in DataTable.


     <Style TargetType="syncfusion:VirtualizingCellsControl">
             <DataTrigger Binding="{Binding Row.RowState}" Value="{x:Static data:DataRowState.Modified}">
                  <Setter Property="Background" Value="LightYellow" />


SfDataGridBehavior :

public class SfDataGridBehavior : Behavior<SfDataGrid>
    protected override void OnAttached()
         (AssociatedObject.ItemsSource as DataTable).RowChanged += SfDataGridBehavior_RowChanged;
    private void SfDataGridBehavior_RowChanged(object sender, DataRowChangeEventArgs e)
         var dataTable = (DataTable)AssociatedObject.ItemsSource;
         var record = AssociatedObject.View.Records.FirstOrDefault(row => (row.Data as DataRowView).Row == e.Row);
         var rowIndex = AssociatedObject.ResolveToRowIndex(record);
         ( System.Windows.Threading.DispatcherPriority. ApplicationIdle,
           new Action(() =>
               if (e.Row.RowState == DataRowState.Modified)
                   var currentRow = AssociatedObject.RowGenerator.Items.FirstOrDefault(r => r.RowIndex == rowIndex);
                   if (currentRow != null)
                        var element = currentRow.Element as VirtualizingCellsControl;
                        element.Background = new SolidColorBrush(Colors.LightYellow);


View Sample in GitHub.

Did you find this information helpful?
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment