How to capture the edited column or row
We could able to capture the "Edited" rows and able to save the datasource back to the database in PivotGrid Control. Please refer the below procedure.
Step 1: Enable the APIs “EnableUpdating” and “EnableValueEditing”.
C#
pivotGrid.EnableValueEditing = true; pivotGrid.EnableUpdating = true;
Step 2: Implement a CustomEditManager class which should implement PivotEditingManager and overrides the method “ChangeValue” which is used to update the newly edited value in datasource.
C#
public class PivotCustomEditManager : PivotEditingManager
{
private PivotGridControl _pivotGrid;
private bool _changeValueHandled = false;
private int lastRow = 0;
private int lastCol = 0;
private object lastVal = 0;
public PivotCustomEditManager(PivotGridControl pg)
: base(pg)
{
_pivotGrid = pg;
}
/// <summary>
/// Override this method to control how the changes affect the pivot display. The base implementation calls IAdjustable methods on the summaries to accomplish the change.
/// </summary>
/// <param name="oldValue">The old value from the cell that was edited.</param>
/// <param name="newValue">The new value from the cell that was edited.</param>
/// <param name="row1">Points to the row index of cell being adjusted. This cell may be different from the cell that was edited.</param>
/// <param name="col1">Points to the column index of cell being adjusted. This cell may be different from the cell that was edited.</param>
/// <param name="pi">The <see cref="PivotCellInfo"/> for the cell being adjusted.</param>
protected override void ChangeValue(object oldValue, object newValue, int row1, int col1, PivotCellInfo pi)
{
if (row1 != _pivotGrid.InternalGrid.CurrentCell.RowIndex || col1 != _pivotGrid.InternalGrid.CurrentCell.ColumnIndex) return;
if (lastRow == row1 && lastCol == col1 && newValue == lastVal) return;
double oldTotal;
double newTotal;
if (!double.TryParse(oldValue.ToString(), out oldTotal) || !double.TryParse(newValue.ToString(), out newTotal))
return;
// Get the raw items that make up the cell that was changed
List<object> rawItems = _pivotGrid.PivotEngine.GetRawItemsFor(row1, col1);
if (rawItems != null && rawItems.Count > 0)
{
SpreadValue(rawItems, "Quantity", oldTotal, newTotal, pi);
}
lastRow = row1;
lastCol = col1;
lastVal = newValue;
_changeValueHandled = true;
}
/// <summary>
/// This method is used to spread the newly edited value to the rawItems so that it will get update in datasource
/// </summary>
private void SpreadValue(List<object> rawItems, string calcFieldName, double oldValue, double newValue, PivotCellInfo pi)
{
// apply some VERY simple spreading for now
foreach (var item in rawItems)
{
var row = ((DataRowView)item).Row;
row[calcFieldName] = newValue / rawItems.Count;
}
}
}
Step 3: Assign the implemented CustomEditManager class to PivotGrid’s EditManager.
C#
pivotGrid.EditManager = new PivotCustomEditManager(pivotGrid);