Skip to main content

Assigning Editors to Individual Cells

  • 3 minutes to read

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

Since all of our editor containers (Data Grid, Tree List, Pivot Grid, etc.) use similar approaches and APIs that allow you to replace default editors, you can also refer to the Data Grid documentation, the Cell Values, Editors, and Validation article.

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 display values of the “Quantity %” field. The ProgressBar editor displays regular cell values as progress bars. The SpinEdit editor allows you to edit total values for the field.

CustomCellEdit_ex

View Example

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);
            // 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