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
375
XamPivotGrid Conditional Formatting CellControlAttached event
posted

Hi,

I am using CellControlAttached event in order to set the foreground color of the xampivotgrid cell as well as format the number. We display a number of products:

e.g. We show Product 1, Product 2 and Product 3  in the same pivot grid view. Out scenario is that we want negative numbers in red as well as format the number based on the product, so in the case of Product 1 we want 4 decimal places, Product 2 we want 2 decimals and Product 3 we want 1. Here is some code to demonstrate this example.

 

                   private void PivotGridCellControlAttached(object sender, PivotCellControlAttachedEventArgs e)
        {
            if (pivotGrid.DataSource == null) return;

            var rowIndex = pivotGrid.DataRows.IndexOf(e.Cell.DataRow);
            var columnIndex = pivotGrid.DataColumns.IndexOf(e.Cell.DataColumn);
            ICell cellData = pivotGrid.DataSource.Result.Cells[rowIndex, columnIndex];

            if (cellData == null) return;
            
            var value = 0.0;
            if (!double.TryParse(cellData.Value.ToString(), out value)) return;
            Style s = null;
            if (value < 0)
                s = Resources["RedStyle"] as Style;
            
            if (e.Cell.Style == null || e.Cell.Style != s)
                e.Cell.Style = s;

            var itemsIndexes = myFlatDataSource.GetCellItemsIndexes(cellData);
            if (itemsIndexes.Count == 0) return;
            var record = myFlatDataSource.GetRecord(itemsIndexes[0]);
            if (record == null) return;
            var myObject = record as MyObjectType;
            var product = myObject.Product;
            if(product == "Product 1")
                        cellData.SetValue(cellData.Value, null, "{0:0.0000}");
            else if (product == "Product 2")
                        cellData.SetValue(cellData.Value, null, "{0:0.00}");
            else if (product == "Product 3")
                        cellData.SetValue(cellData.Value, null, "{0:0.0}");
            
        }

 

The RedStyle gets applied correctly to all the cells, but the number format gets applied to all values currently out of view. The cells displayed in my intial view only apply the format if i scroll the cell out of veiw and then scroll it back into view.

I am guessing that this is due to the virtualisation of the grid, could you let me know how i can get around it please.

Thanks,

Nick

Parents Reply Children