Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
100
Change Background Color of a Record
posted

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?

Parents
  • 339
    posted

    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

Reply Children
No Data