How to: Assign In-place Editors to Cells

  • 3 minutes to read

The following code shows how to handle the PivotGridControl.CustomCellEdit event, to assign different in-place editors to different types of cells.

In the example, two in-place editors (repository items) are created to represent values of the "Quantity %" field. A ProgressBar editor is used to represent regular cell values, while a SpinEdit editor is used to represent total values for this field:

CustomCellEdit_ex

using System;
using System.Windows.Forms;
using DevExpress.Data.PivotGrid;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraPivotGrid;

namespace PivotGridControl_CustomCellEdit {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        // Creates new repository items.
        RepositoryItemProgressBar riProgressBar = new RepositoryItemProgressBar();
        RepositoryItemSpinEdit riSpinEdit = new RepositoryItemSpinEdit();

        private void Form1_Load(object sender, EventArgs e) {
            // Binds the pivot grid to data.
            this.salesPersonTableAdapter.Fill(this.nwindDataSet.SalesPerson);

            // Specifies the type of data field and format settings.
            fieldQuantityPercent.SummaryDisplayType = PivotSummaryDisplayType.PercentOfColumn;
            fieldQuantityPercent.CellFormat.FormatType = DevExpress.Utils.FormatType.Custom;
            fieldQuantityPercent.CellFormat.FormatString = "{0}%";

            // Initializes cell editors used to represent values of regular and total cells respectively.
            pivotGridControl1.RepositoryItems.AddRange(new RepositoryItem[] { riProgressBar, riSpinEdit });

            pivotGridControl1.CustomCellEdit += new 
                EventHandler<PivotCustomCellEditEventArgs>(pivotGridControl1_CustomCellEdit);
        }

        private void pivotGridControl1_CustomCellEdit(object sender, PivotCustomCellEditEventArgs e) {
            // Specifies editors for cells depending on a cell type.
            if (e.DataField == fieldQuantityPercent) {
                if (e.RowValueType == PivotGridValueType.Value)
                    e.RepositoryItem = riProgressBar;

                if (e.RowValueType == PivotGridValueType.GrandTotal)
                    e.RepositoryItem = riSpinEdit;
            }
        }

        private void pivotGridControl1_CustomCellValue(object sender, PivotCellValueEventArgs e) {
            if (e.DataField == fieldQuantityPercent)
                e.Value = Convert.ToDecimal(e.Value) * 100;
        }

    }
}