When my grid loads, I wish to interrogate every record. Based on a certain condition (Cell value in one column matched against another column), I want to set the background color of the record.
I wish to do this programmically in c#. Thank you.
How do I go about doing this?
Hello,
This is how I achieved dynamic row background colors using the DataGrid - I'm assuming it would work the same way with the DataPresenter:
First, you need to implement some XAML, this could probably be achieved in C# but it was a cleaner approach for me to go down the XAML route:
You need to create a Style that contains a Setter property of type Background, then set its value to be bound to a IValueConverter Class (see further down).
<x.Resources>... <Style x:Key="drpRowStyle" TargetType="{x:Type igDP:DataRecordCellArea}"> <Setter Property="Background" Value="{Binding Converter={StaticResource rowBackgroundColorHelper}}" /> </Style>...</x.Resources>
Now override the default Style of each Data Record Cell Area :<igDP:FieldLayoutSettings DataRecordCellAreaStyle="{StaticResource drpRowStyle}" />
Also, add a reference in XAML to the IValueConverter Class, that I have called "rowBackgroundColorHelper" :<local:RowBackgroundColorHelper x:Key="rowBackgroundColorHelper" />
Here's the C# implementation of IValueConverter to return a SolidColorBrush :
public class RowBackgroundColorHelper : IValueConverter {
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { SolidColorBrush brush = Brushes.Transparent;
DataRecord dr = value as DataRecord;
return brush; }
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } }
You can see that the Convert method is inherently passed the associated DataRecord object based on the row you are dealing with. You can then query the Cells property to get the value of individual cells etc.
Hope this helps.
Jamie
Hi ,
I saw that you are changing the background of the field in the grid using the IValueConverter .
Did you get a chance to use IMultiValueConverter to set the background of the Field depending on the values of the other fields in the same row.
Thanks
Prakash
For anyone coming to this thread, the solution is here
Alex.