Back to Devexpress

Assigning Editors to Individual Cells

windowsforms-5895-controls-and-libraries-pivot-grid-data-shaping-editing-assigning-editors-to-individual-cells.md

latest7.3 KB
Original Source

Assigning Editors to Individual Cells

  • Oct 12, 2020
  • 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.

View Example

cs
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;
        }
    }
}
vb
Imports System
Imports System.Windows.Forms
Imports DevExpress.XtraEditors.Repository
Imports DevExpress.XtraPivotGrid

Namespace PivotGridControl_CustomCellEdit
    Partial Public Class Form1
        Inherits Form

        Public Sub New()
            InitializeComponent()
        End Sub

        ' Creates new repository items.
        Dim riProgressBar As New RepositoryItemProgressBar()
        Dim riSpinEdit As New RepositoryItemSpinEdit()

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            ' Binds the pivot grid to data.
            Me.salesPersonTableAdapter.Fill(Me.nwindDataSet.SalesPerson)

            ' Initializes cell editors used to represent values of regular and total cells respectively.
            pivotGridControl1.RepositoryItems.AddRange(New RepositoryItem() {riProgressBar, riSpinEdit})
        End Sub

        Private Sub pivotGridControl1_CustomCellEdit(ByVal sender As Object,
                            ByVal e As PivotCustomCellEditEventArgs) Handles pivotGridControl1.CustomCellEdit
            ' Specifies editors for cells depending on a cell type.
            If e.DataField Is fieldQuantity Then
                If e.RowValueType = PivotGridValueType.GrandTotal Then
                    e.RepositoryItem = riSpinEdit
                End If
                If e.RowValueType = PivotGridValueType.Value Then
                    e.RepositoryItem = riProgressBar
                End If
            End If
        End Sub

        Private Sub pivotGridControl1_CustomCellValue(ByVal sender As Object,
                            ByVal e As PivotCellValueEventArgs) Handles pivotGridControl1.CustomCellValue
            If e.DataField Is fieldQuantity Then
                e.Value = Convert.ToDecimal(e.Value) * 100
            End If
        End Sub
    End Class
End Namespace

See Also

Assigning Editors to Data Field's Cells

Assigning Editors for In-place Editing