Welcome to the Xceed Community | Help
Community Search  
More Search Options

Dynamic / Conditional formating of cell background color?

Sort Posts: Previous Next
  •  04-26-2009, 11:38 AM Post no. 20555

    Dynamic / Conditional formating of cell background color?

    Hi,

    I am trying to change the background color of individual cells based on their values.  For example, one of my columns (I'm binding to a DataTable) is of type DateTime and I want the background color of those cells to turn red when they are less than 1 hour from the current time.  I also have other Columns that are of type int and would like them to change colors base on their values.

    I've been able to accomplish the changing of the cell background color in a static way using this type of logic:

    <DataTrigger Binding="{Binding Path=status}" Value="Active">

    <Setter Property="Background" Value="Red" />

    </DataTrigger>

    However, I would like to create a method (converter?) in the C# code behind that takes care of this for me.  The C# method isn't a problem but I can't quite figure out how to properly interface this with my Xceed Datagrid. 

    1. Do I attach it to some event handeler?  If so.. which one (I want the color logic to always be applied... my grid is read-only.. and gets updated once an hour).  Do I use a converter?  If so.. how does it get attached?
    2. I would need the method to determine the correct color an return that to the grid.  The logic to determine the color is no problem.. but how do I send the color back to the grid? Any Ideas?

    Also, a somewhat advanced question.... Can the backgroud color of sells be set using a gradient brush?  Can the colors be defined using their

     Thank You to anyone who takes the time to help.

    Stephen

  •  04-27-2009, 2:26 AM Post no. 20556 in reply to 20555

    Re: Dynamic / Conditional formating of cell background color?

    The way to implement would be to set your Value to being a binding and call a converter at the same time. Typically in XAML:

    In the resources section, declare the converter class with a key -

    <f:CLS_MyConverter x:Key="BackgroundColourConverter"/>

    In your cell style, set the background as follows -

    <Style TargetType="{x:Type xgdg:DataCell}">

    <Setter Property="Background" Value={Binding xxx, Converter={StaticResource BackgroundColourConverter}}"/>

    </Style>

     The xxx depends on what you want in your converter class to determine the colour (ie do you just need a single value, the whole row. If you need to pass multiple values then you will need a MultiConverter.

    The converter should be declared as -

    CLS_MyConverter : IValueConverter

    {

    //In the Convert method, you should use the passed value object to determine the colour of the cell you want. Return should be the Brush you want to use (I only have experience of returning a SolidColorBrush.

    //ConvertBack method - I have not had need to use for background colours so I normally return 0 or null as appropriate.

    }

     

    Let me know if you need more info but this should get you going in the right direction.

  •  04-28-2009, 9:22 PM Post no. 20616 in reply to 20556

    Re: Dynamic / Conditional formating of cell background color?

    Hi

    I'm trying to accomplish something similar to Stephen.

    col1   col2   col3

    A       7        3

    B       8        3

    C       5        4  

    D       5        6

    From Col2 to col3, I want to compare value in each cell and the value in the next cell in the same column for every two rows(A&B; C&D), if they are different, background color should be red. So in the end, 7,8,4 and 6 cells should be in red.

    Can you please suggest me how to implement this?

    Thanks

    Raghav

  •  08-21-2009, 2:41 PM Post no. 23471 in reply to 20616

    Re: Dynamic / Conditional formating of cell background color?

    I am wishing to do the exact same thing as Raghav.  Raghav, did you find a way to do this eventually?

    If not, Xceed Support, is it possible you could advise of a way of programmatically doing this or even through Style Setters in XAML?

    I have a further requirement that I wish to do what Raghav requires but within each Group of a DataGridControl (dgc).  So, for example:

    foreach (ReadOnlyCollection<object> group in dgc.Items.Groups)
    {
        for (int x = 0; x < group.Count; x++)
        {
            foreach (Column column in dgc.Columns)
            {
                If column value of group[x] != column value of group[x+1] Then
                    Cell ForeColor of group[x] under column = Red;

            }
        }
    }

    The blue code above is obviously pseudo-code but that should hopefully convey what I'm trying to do.

    Many thanks for any help you can give...

    Jason


    Associate, .NET Development
    Morgan Stanley, UK
  •  08-22-2009, 2:27 AM Post no. 23474 in reply to 20555

    Re: Dynamic / Conditional formating of cell background color?

     

    Kindly find the below code for the dynamically adding style and the Data Triggers for the changing the backgroud colour based on the Database FieldName value:

    Style stDataCell = new Style(typeof(Xceed.Wpf.DataGrid.DataCell));

     DataTrigger ds = new DataTrigger();
                        Binding bs = new Binding("FieldName");
                        bs.RelativeSource = RelativeSource.Self;
                        ds.Binding = bs;
                        ds.Value = FieldName;
                        ds.Setters.Add(new Setter(BackgroundProperty, Brushes.Aqua));
                        stDataCell.Triggers.Add(ds);
                        stColumnManagerCell.Triggers.Add(ds);

    this.Resources[typeof(Xceed.Wpf.DataGrid.DataCell)] = stDataCell;

     

    You can dynamically change the Cell Colour according to the Data and the way you want the colour to be.

     

    Thank You,

    Paras Sanghani


    Nothing Is Impossible
  •  08-25-2009, 10:54 AM Post no. 23509 in reply to 23474

    Re: Dynamic / Conditional formating of cell background color?

    I tried this, but had to obtain stColumnManagerCell using the following code:

    ColumnManagerRow columnManagerRow = FindVisualChild<ColumnManagerRow>(dgcMyData);

    stColumnManagerCell = columnManagerRow.Cells["MyProp1"];
    // The following lines of code were each tried separately, but all resulted in errors
    //stColumnManagerCell.Style = style;
    //stColumnManagerCell.Triggers.Add(trigger);
    //columnManagerRow.Triggers.Add(trigger);

    And the code for FindVisualChild obtained from http://msdn.microsoft.com/en-us/library/bb613579.aspx

    As written in the comment in the code above, I kept getting run-time errors, any idea on how to resolve this?


    Associate, .NET Development
    Morgan Stanley, UK
  •  09-22-2009, 3:43 PM Post no. 24094 in reply to 23509

    Re: Dynamic / Conditional formating of cell background color?

    Please see my latest thread on this complete with example as I am trying to achieve similar but using XAML:

    http://xceed.com/CS/forums/thread/23535.aspx


    Associate, .NET Development
    Morgan Stanley, UK
  •  03-11-2010, 6:46 PM Post no. 26081 in reply to 24094

    Re: Dynamic / Conditional formating of cell background color?

    Have a look at the solution here: http://xceed.com/CS/forums/thread/26080.aspx
    Associate, .NET Development
    Morgan Stanley, UK
View as RSS news feed in XML
Contact | Site Map | Reviews | Legal Terms of Use | Trademarks | Privacy Statement Copyright 2011 Xceed Software Inc.