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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Thread ID:

Created:

Updated:

Platform:

Replies:

149024 Nov 12,2019 08:40 AM UTC Nov 12,2019 11:47 AM UTC WPF 3
loading
Tags: SfDataGrid
Michal
Asked On November 12, 2019 09:15 AM UTC

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.

Shobika Palani [Syncfusion]
Replied On 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. 


Michal
Replied On November 12, 2019 10:44 AM UTC

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

Farjana Parveen Ayubb [Syncfusion]
Replied On 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 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon