Example T124603
Visible to All Users

Grid View for ASP.NET MVC - How to calculate values dynamically in batch edit mode

This example demonstrates how to create an unbound column (Sum) that changes its values based on other column values dynamically in batch edit mode.

Calculate values on dynamically

Overview

Follow the steps below:

  1. 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; });
  2. 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

GridViewBatchEdit/Views/Home/_GridViewPartial.cshtml
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()
GridViewBatchEdit/Views/Home/Index.cshtml
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")

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.