Assigning Editors to Individual Cells

  • 4 minutes to read

This topic explains how to provide in-place editors for individual cells. For general information, see Cell Editors Overview.

Assigning Editors to Individual Cells

To assign editors to individual cells, handle the PivotGridControl.CustomCellEdit event. This event occurs dynamically for each visible cell and specifies editors for individual cells depending on the cell's position and type. The editor that assigned using this event represents cell contents in display mode, and by default, is used for in-place editing. To provide a different editor to be used in edit mode, refer to Assigning Editors for In-place Editing for more details.

The PivotGridControl.CustomCellEdit event's parameters allow you to identify the cell being currently processed. To provide an editor for the currently processed cell, assign a specific repository item to the event's RepositoryItem parameter. You need to add the repository items to the Pivot Grid's repository (the DevExpress.XtraPivotGrid.PivotGridControl.RepositoryItems collection).

Refer to the Editors and Simple Controls topic for details on repository technology.

NOTE

The PivotGridControl.CustomCellEdit event is raised when view information is recalculated. There are times, however, you may need to force editor reassignment manually. To do so, call the PivotGridControl.LayoutChanged method.

Example

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;
        }

    }
}
See Also