We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

How do I refresh the row background color as soon as the row is edited?

I have a SfDataGrid with an editable list that changes color depending on current data, like this:

     
          
          
     

     public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
var data = value as Product;

if (data.IsIgnored == true)
return new SolidColorBrush(Colors.DarkGray);
else if (data.IsInDelivery == true)
return new SolidColorBrush(Colors.LightGreen);
else
return DependencyProperty.UnsetValue;
}

I'm not sure where I got it from, needless to say it is not my "invention". Edit: It's official documentation, here: https://help.syncfusion.com/wpf/datagrid/conditional-styling?cs-save-lang=1&cs-lang=xaml#conditional-styling-of-cells-using-converter

My issue is that it works, only that the colors don't update until I scroll down the list and up to the top again. I tried refreshing the list with

     sfdatagrid.View.Refresh();

but it didn't work at all.

5 Replies

SP Shobika Palani Syncfusion Team November 12, 2019 10:28 AM UTC

Hi Michal, 
 
Thank you for contacting Syncfusion support. 
 
We have analyzed your query to refresh row background when row is edited. This requirement can be achieved by using RowStyleSelector  or Style.Triggers.  
 
Method 1: Using RowStyleSelector 
 
You can modify the background of the rows using StyleSelector based on the cell values at the time of loading. To update row background at runtime, you need to call the UpdateDataRow method in CurrentCellEndEdit event. Please refert to the code snippet below 
 
C#: 
public class RowStyleSelector : StyleSelector 
    { 
        public override Style SelectStyle(object item, DependencyObject container) 
        { 
            var record = (item as DataRow).RowData as Model; 
            if (record == null) 
                return base.SelectStyle(item, container); 
            if (record.IsIgnored) 
            { 
                return App.Current.Resources["IgnoredStyle"] as Style; 
            } 
            else if(record.IsInDelivery) 
            { 
                return App.Current.Resources["DeliveredStyle"] as Style; 
            } 
            return base.SelectStyle(item, container); 
        } 
    } 
 
public class SfDataGridBehavior : Behavior<SfDataGrid> 
    { 
        protected override void OnAttached() 
        { 
            base.OnAttached(); 
            this.AssociatedObject.CurrentCellValueChanged += AssociatedObject_CurrentCellValueChanged; 
            this.AssociatedObject.CurrentCellEndEdit += AssociatedObject_CurrentCellEndEdit; 
        } 
        bool isValueChanged = false; 
        private void AssociatedObject_CurrentCellEndEdit(object sender, CurrentCellEndEditEventArgs e) 
        { 
            if (!isValueChanged) 
                return; 
            var grid = sender as SfDataGrid; 
            //getting GridCell 
            var cell = 
                (grid.SelectionController.CurrentCellManager.CurrentCell.Renderer as GridCellRendererBase) 
                    .CurrentCellElement; 
 
            if (cell != null) 
            { 
                grid.UpdateDataRow(e.RowColumnIndex.RowIndex); 
            } 
            isValueChanged = false; 
        } 
 
        private void AssociatedObject_CurrentCellValueChanged(object sender, CurrentCellValueChangedEventArgs e) 
        { 
            isValueChanged = true; 
        } 
    } 
 
 
XAML: 
<Application.Resources> 
        <Style TargetType="syncfusion:VirtualizingCellsControl" x:Key="IgnoredStyle"> 
            <Setter Property="Background" Value="DarkGray"/> 
        </Style> 
        <Style TargetType="syncfusion:VirtualizingCellsControl" x:Key="DeliveredStyle"> 
            <Setter Property="Background" Value="LightGreen"/> 
        </Style> 
    </Application.Resources> 
 
<local:RowStyleSelector x:Key="rowStyleSelector"/> 
 
<syncfusion:SfDataGrid x:Name="sfdatagrid" AutoGenerateColumns="False"                           
                       AllowEditing="True"  
                      RowStyleSelector="{StaticResource rowStyleSelector}" 
                               ItemsSource="{Binding OrderInfoCollection}" > 
 
 
 
Please refer to the sample below 
 
Sample Link: 
 
Method 2 : Using Style.Triggers 
Using Style.Triggers, you can bind the values directly to DataTrigger depending on which you are changing the row background. Please refer to the below code snippet 
 
<Window.Resources> 
        <Style TargetType="syncfusion:VirtualizingCellsControl"> 
            <Style.Triggers> 
                <DataTrigger Binding="{Binding IsIgnored,                            
                  UpdateSourceTrigger=PropertyChanged}" Value="True"> 
                    <Setter Property="Background" Value="DarkGray"/> 
                </DataTrigger > 
                <DataTrigger Binding="{Binding IsInDelivery,                            
                  UpdateSourceTrigger=PropertyChanged}" Value="True" > 
                    <Setter Property="Background" Value="LightGreen"/> 
                </DataTrigger> 
            </Style.Triggers> 
        </Style> 
   </Window.Resources> 
 
 
Please refer to the sample for the same in the below link 
Sample Link: 
 
Also please refer to the below KB articles to know more details on styling 
KB Links: 
 
Please let us know, if you need any further assistance on this. 
 
Regards, 
Shobika. 



MI Michal November 12, 2019 10:44 AM UTC

Thanks, it works. Method 2 is super simple yet it did exactly what I wanted.


FP Farjana Parveen Ayubb Syncfusion Team November 12, 2019 11:47 AM UTC

Hi Michal 
 
Thanks for the update. 
 
We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. We are happy to help you. 
 
Regards, 
Farjana Parveen A 



WG Walter Grimm January 20, 2022 06:15 PM UTC

HI 

I tried the first sample, but using current VS 2019 with net 4.72 I am getting a lot of errors, and cant get the sample running.



MA Mohanram Anbukkarasu Syncfusion Team January 21, 2022 02:47 PM UTC

Hi Walter,  

We have modified the sample to .Net Framework 4.7.2 and it is available in the following link for your reference.  


Please have a look at this sample and revert to us with more details about the errors if you are still facing the issues. It will help us to the exact cause for the errors and to provide a prompt solution.  

Regards, 
Mohanram A. 


Loader.
Live Chat Icon For mobile
Up arrow icon