This example demonstrates how to create an unbound column (Sum) that changes its values based on other column values dynamically in batch edit mode.
Overview
Follow the steps below:
- Set the unbound column's ShowEditorInBatchEditMode property to
false
to make the column read-only in batch edit mode.C#settings.Columns.Add(column => { column.UnboundType = DevExpress.Data.UnboundColumnType.Decimal; column.FieldName = "Sum"; column.ReadOnly = true; column.Settings.ShowEditorInBatchEditMode = false; });
- Handle the grid's client-side BatchEditEndEditing event. In the handler, recalculate column values based on the changes and call the SetCellValue method to set the new column value.JavaScript
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); }
Files to Review
Documentation
More Examples
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
Razor@{
var grid = Html.DevExpress().GridView(settings =>
{
settings.Name = "GridView";
settings.CallbackRouteValues = new { Controller = "Home", Action = "GridViewPartial" };
settings.SettingsEditing.BatchUpdateRouteValues = new { Controller = "Home", Action = "BatchUpdatePartial" };
settings.SettingsEditing.Mode = GridViewEditingMode.Batch;
settings.CommandColumn.Visible = true;
settings.CommandColumn.ShowDeleteButton = true;
settings.CommandColumn.ShowNewButtonInHeader = true;
settings.KeyFieldName = "ID";
settings.ClientSideEvents.BatchEditEndEditing = "OnBatchEditEndEditing";
settings.Columns.Add(column =>
{
column.FieldName = "Quantity";
column.ColumnType = MVCxGridViewColumnType.SpinEdit;
SpinEditProperties prop = column.PropertiesEdit as SpinEditProperties;
prop.MinValue = 0;
prop.MaxValue = 9999;
});
settings.Columns.Add(column =>
{
column.FieldName = "Price";
column.ColumnType = MVCxGridViewColumnType.SpinEdit;
SpinEditProperties prop = column.PropertiesEdit as SpinEditProperties;
prop.MinValue = 0;
prop.MaxValue = 9999;
});
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;
}
};
settings.CellEditorInitialize = (s, e) =>
{
ASPxEdit editor = (ASPxEdit)e.Editor;
editor.ValidationSettings.Display = Display.Dynamic;
};
});
if (ViewData["EditError"] != null)
{
grid.SetEditErrorText((string)ViewData["EditError"]);
}
}
@grid.Bind(Model).GetHtml()
Razor<script type="text/javascript">
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);
}
</script>
@Html.Action("GridViewPartial")