Trigger RowStyle or CellStyle Update When Underlying Data Changes

I have defined a RowStyleSelector which sets the background color of a row based on the value of a property in the data object underlying the row. It works great when the SfDataGrid is initially populated.

However, I need to be able to "re-trigger" the style selection when a property in that data object changes at runtime (it's an observable property which raises the INotifyPropertyChanged event). Unfortunately, I haven't been able to figure out a way to do that.

I suppose, if nothing else is available, I could simply clear and recreate the list of data objects. But that seems kind of kludgy to me.

Is there a way to trigger RowStyleSelector when the underlying data changes?


4 Replies

VS Vijayarasan Sivanandham Syncfusion Team March 10, 2022 02:02 PM UTC

Hi Mark,

Currently, we are analyzing your requirement of “trigger RowStyleSelector when the underlying data changes in SfDataGrid” We will validate and update you the details on or before March 14, 2022.

We appreciate your patience until then.

Regards,
Vijayarasan S



VS Vijayarasan Sivanandham Syncfusion Team March 14, 2022 09:30 PM UTC

Hi Mark,

We are still working on this. We will update you with further details on or before March 16, 2022.

We appreciate your patience and understanding.

Regards,
Vijayarasan S





VS Vijayarasan Sivanandham Syncfusion Team March 16, 2022 04:39 PM UTC

Hi Mark,

We are still working on this. We will update you with further details on or before March 18, 2022.

We appreciate your patience and understanding.

Regards,
Vijayarasan S


VS Vijayarasan Sivanandham Syncfusion Team March 18, 2022 07:33 PM UTC

Hi Mark,

Your requirement to update the RowStyle or CellStyle while modifying the underlying data at runtime in SfDataGrid can be achieved by calling the UpdateDataRow method in CurrentCellEndEdit and View.RecordPropertyChanged event in SfDataGrid. Please refer the below code snippet,

XAML Code Snippet:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
ResourceDictionary.MergedDictionaries>
<local:CustomRowStyleSelector x:Key="rowStyleSelector" />
<Style x:Key="rowStyle1" TargetType="syncfusion:DataGridRowControl">
<Setter Property="Background" Value="Bisque" />
Style>
<Style x:Key="rowStyle2" TargetType="syncfusion:DataGridRowControl">
<Setter Property="Background" Value="Aqua" />
Style>
ResourceDictionary>
Application.Resources>


C# Code Snippet of UpdateDataRow:

private void OnLoaded(object sender, RoutedEventArgs e)
{
this.dataGrid.View.RecordPropertyChanged += OnRecordPropertyChanged;
}
private void OnRecordPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
//Change the RowStyleSelector or CellStyleSelector at runtime by update the value changed row index in SfDataGrid
UpdateDataRow(index);
}
private void OnCurrentCellEndEdit(object sender, Syncfusion.UI.Xaml.DataGrid.CurrentCellEndEditEventArgs e)
{
index = e.RowColumnIndex.RowIndex;
//Change the RowStyleSelector or CellStyleSelector at runtime by update the value changed row index in SfDataGrid
UpdateDataRow(index);
}
public void UpdateDataRow(int index)
{
var visualcontainer = this.dataGrid.GetType().GetProperty("VisualContainer", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(this.dataGrid) as VisualContainer;
if (visualcontainer == null)
return;
var datarow = this.dataGrid.RowGenerator.Items.FirstOrDefault(row => row.RowIndex == index);
if (datarow != null)
{
var rowIndex = datarow.GetType().GetProperty("RowIndex", BindingFlags.Instance | BindingFlags.Public);
rowIndex.SetValue(datarow , -1);
visualcontainer.InvalidateMeasureInfo();
visualcontainer.InvalidateArrange();
}
}


C# Code Snippet of RowStyle:

public class CustomRowStyleSelector : StyleSelector
{
protected override Style SelectStyleCore(object item, DependencyObject container)
{
var row = (item as DataRowBase).RowData;
var data = row as ProductSalesDetails;
if (data.Quantity < 6)
return App.Current.Resources["rowStyle1"] as Style;
return App.Current.Resources["rowStyle2"] as Style;
}
}



Loader.
Up arrow icon