Current filter:
                                You should refresh the page.
                                Support Center

                                How to customize appearance of the Even/Odd groups of cells.

                                -1

                                We have a few data fields in the pivot grid control. We want to apply a custom appearance to a set of cells, corresponding to the same Field Values. Even and odd sets of cells should be colorized with different colors.
                                The easiest way to accomplish this task is to compare Field Values, corresponding to a processed cell, with Field Values, corresponding to a previous cell. We use the GetFieldValue method to get Field Value by a column index.
                                This example is made with the XtraPivotGrid control. However, the same approach can be used with the ASPxPivotGrid and DXPivotGrid controls as well.

                                You must  log in  or  register  to leave comments
                                Select file
                                • Form1.cs
                                • Program.cs
                                Select language
                                • C#
                                • VB.NET
                                Select version
                                • v2013 vol 1.4 - v2013 vol 2.9
                                • v2010 vol 2.8 - v2012 vol 2.16
                                using System;
                                using System.Collections.Generic;
                                using System.ComponentModel;
                                using System.Data;
                                using System.Drawing;
                                using System.Text;
                                using System.Windows.Forms;
                                using DevExpress.XtraPivotGrid;
                                
                                namespace WindowsApplication53
                                {
                                    public partial class Form1 : Form
                                    {
                                        public Form1()
                                        {
                                            InitializeComponent();
                                        }
                                        private void Form1_Load(object sender, EventArgs e)
                                        {
                                            PopulateTable();
                                            pivotGridControl1.RefreshData();
                                            pivotGridControl1.BestFit();
                                        }
                                        private void PopulateTable()
                                        {
                                            DataTable myTable = dataSet1.Tables["Data"];
                                            myTable.Rows.Add(new object[] { "Aaa", DateTime.Today, 7, 3 });
                                            myTable.Rows.Add(new object[] { "Aaa", DateTime.Today.AddDays(1), 4, 1 });
                                            myTable.Rows.Add(new object[] { "Bbb", DateTime.Today, 12, 6 });
                                            myTable.Rows.Add(new object[] { "Bbb", DateTime.Today.AddDays(1), 14, 7 });
                                            myTable.Rows.Add(new object[] { "Ccc", DateTime.Today, 11, 1 });
                                            myTable.Rows.Add(new object[] { "Ccc", DateTime.Today.AddDays(1), 10, 3 });
                                
                                            myTable.Rows.Add(new object[] { "Aaa", DateTime.Today.AddYears(1), 4, 5 });
                                            myTable.Rows.Add(new object[] { "Aaa", DateTime.Today.AddYears(1).AddDays(1), 2, 8 });
                                            myTable.Rows.Add(new object[] { "Bbb", DateTime.Today.AddYears(1), 3, 9 });
                                            myTable.Rows.Add(new object[] { "Bbb", DateTime.Today.AddDays(1).AddYears(1), 1, 5 });
                                            myTable.Rows.Add(new object[] { "Ccc", DateTime.Today.AddYears(1), 8, 3 });
                                            myTable.Rows.Add(new object[] { "Ccc", DateTime.Today.AddDays(1).AddYears(1), 22 });
                                        }
                                
                                        private void pivotGridControl1_CustomAppearance(object sender, DevExpress.XtraPivotGrid.PivotCustomAppearanceEventArgs e)
                                        {
                                            int groupIndex = GetGroupIndex((PivotGridControl)sender, e);
                                            if (groupIndex % 2 == 0)
                                                e.Appearance.BackColor = Color.GreenYellow ;
                                        }
                                
                                        private int GetGroupIndex(PivotGridControl pivot, PivotCustomAppearanceEventArgs e)
                                        {
                                            PivotGridField[] columnFields = pivot.GetFieldsByArea(PivotArea.ColumnArea).ToArray();
                                            int groupIndex = -1;
                                            object[] previous = new object[ columnFields.Length ];
                                            object[] current = new object[columnFields.Length]; 
                                            GetValuesByIndex(pivot, columnFields, previous, 0);
                                            for (int i = 1; i <= e.ColumnIndex ; i++)
                                            {
                                                GetValuesByIndex(pivot, columnFields, current, i);
                                                if (! ValuesEquals( previous,current))
                                                {
                                                    groupIndex++;
                                                    current.CopyTo(previous, 0);
                                                }
                                            }
                                            List<object> o = new List<object>();
                                            return groupIndex;
                                        }
                                
                                        private bool ValuesEquals(object[] previous, object[] current)
                                        {
                                            for (int i = 0; i < previous.Length ; i++)
                                            {
                                                if (!Equals(previous[i], current[i]))
                                                    return false;
                                            }
                                            return true;
                                        }
                                
                                        private void GetValuesByIndex(PivotGridControl pivot, PivotGridField[] fields, object[] current, int index)
                                        {
                                            for (int i = 0; i < fields.Length ; i++)
                                            {
                                                current[i] = pivot.GetFieldValue(fields[i], index);
                                            }
                                        }
                                
                                
                                
                                    }
                                }

                                To start a chat you should create a support ticket


                                If you need additional product information, write to us at info@devexpress.com or call us at +1 (818) 844-3383

                                FOLLOW US

                                DevExpress engineers feature-complete Presentation Controls, IDE Productivity Tools, Business Application Frameworks, and Reporting Systems for Visual Studio, along with high-performance HTML JS Mobile Frameworks for developers targeting iOS, Android and Windows Phone. Whether using WPF, Silverlight, ASP.NET, WinForms, HTML5 or Windows 8, DevExpress tools help you build and deliver your best in the shortest time possible.

                                Copyright © 1998-2014 Developer Express Inc.
                                All trademarks or registered trademarks are property of their respective owners