This example demonstrates how to create an unbound column that calculates the sum of other columns and changes its values on the fly when end-user changes any grid values using Batch edit mode.
You can find detailed steps by clicking below the "Show Implementation Details" link .
See Also:
GridView - Batch Edit - How to calculate unbound column and total summary values on the fly
ASP.NET Web Forms Example:
ASPxGridView - Batch Edit - How to calculate values on the fly
Starting with v16.1, it's possible to modify cells without highlighting using the corresponding ASPxClientGridViewBatchEditApi.SetCellValue method overload and make a column non-editable for a user using the GridDataColumnSettings.ShowEditorInBatchEditMode property.
To implement the required task, perform the following steps:
1. Create an unbound column in the same manner as described in the Unbound Columns help article:
settings.Columns.Add(column =>{
column.UnboundType = DevExpress.Data.UnboundColumnType.Decimal;
column.FieldName = "Sum";
column.ReadOnly = true;
column.Settings.ShowEditorInBatchEditMode = false;
});
settings.CustomUnboundColumnData = (sender, e) =>
{
if (e.Column.FieldName == "Sum") {
decimal price = Convert.ToDecimal(e.GetListSourceFieldValue("Price"));
int quantity = Convert.ToInt32(e.GetListSourceFieldValue("Quantity"));
e.Value = price * quantity;
}
};
2. Handle the ASPxClientGridView.BatchEditEndEditing event to re-calculate the values based on the new changes and set it to the unbound column using the ASPxClientGridViewBatchEditApi.SetCellValue method:
function OnBatchEditEndEditing(s, e) {
var PriceColIndex = s.GetColumnByField("Price").index;
var QuantityColIndex = s.GetColumnByField("Quantity").index;
var priceValue = e.rowValues[PriceColIndex].value;
var quantityValue = e.rowValues[QuantityColIndex].value;
s.batchEditApi.SetCellValue(e.visibleIndex, "Sum", priceValue * quantityValue, null, true);
}