Back to Devexpress

Grid in Batch Edit Mode

aspnet-16443-components-grid-view-concepts-edit-data-batch-edit-mode.md

latest25.9 KB
Original Source

Grid in Batch Edit Mode

  • Feb 02, 2024
  • 10 minutes to read

In batch edit mode, ASPxGridView allows users to modify data in batches and send them to the server in one request.

To enable the batch edit mode, set the ASPxGridView.SettingsEditing.Mode property to Batch.

aspx
<dx:ASPxGridView ID="Grid" runat="server" ClientInstanceName="ClientGrid" ...>
    <!-- ... -->
    <SettingsEditing Mode="Batch" />
</dx:ASPxGridView>

Run Demo: ASPxGridView - Batch Edit Mode

Watch Video: Batch Editing in ASPxGridView

Edit Data in the UI

In batch edit mode, a user focuses a cell and clicks it to start editing. To change the user action that shows an editor, specify the grid’s StartEditAction property.

aspx
<SettingsEditing Mode="Batch">
    <BatchEditSettings StartEditAction="Click" />
</SettingsEditing>

The image below illustrates the grid behavior when its EditMode property is set to Cell – the Grid View shows an editor for one focused cell.

If you set the control’s EditMode property to Row, the grid shows in-place editors for all editable cells in a row.

aspx
<SettingsEditing Mode="Batch">
    <BatchEditSettings EditMode="Row"/>
</SettingsEditing>

To insert and delete rows, the grid displays the New and Delete command buttons. To specify the visibility of these buttons, use the ShowNewButton, ShowNewButtonInHeader, and ShowDeleteButton properties.

aspx
<Columns>
    <dx:GridViewCommandColumn ShowDeleteButton="true" ShowNewButtonInHeader="true" />
    <!-- ... -->
</Columns>

The grid also allows users to use a keyboard to navigate through data cells and switch them to edit mode.

KeyAction
Arrow KeysMove focus between cells.
TabMoves focus to the next cell.
Shift+TabMoves focus to the previous cell.
EnterStarts and ends editing.
F2Starts editing.
DeleteMarks the current row as deleted.

Disable Cell/Row Editing

To prevent a user from editing a cell or row in ASPxGridView, you can set the cancel property to true in a BatchEditStartEditing event handler.

aspx
<ClientSideEvents BatchEditStartEditing="StartEditing" />
js
function StartEditing(s, e) {
    if (e.focusedColumn.fieldName === "Country")
        e.cancel = true;
}

When the control’s EditMode property is set to Row, the grid shows in-place editors for all editable cells in the row. To delete the specified cell from the range of editable cells, use the rowValues argument property in a BatchEditStartEditing event handler.

js
function StartEditing(s, e) {
    var columnIndex = ClientGrid.GetColumnByField('Country').index;
    delete e.rowValues[columnIndex];

Disable Cell Focus

To prevent a user from focusing a cell, you can set the cancel property to true in a handler of the grid control’s FocusedCellChanging event.

js
function onFocusedCellChanging(s, e) {
    visibleRowIndex = e.cellInfo.rowVisibleIndex;
    if (e.cellInfo.column.fieldName === "Country") {
        e.cancel = true;
    }
}

View Example: How to enable or disable the cell edit functionality in batch mode based on a condition

Edit Data in Code

Use the following ASPxClientGridViewBatchEditApi members to modify data batches in code:

MemberTypeDescription
BatchEditRowInsertingEventFires before a data row is inserted in batch edit mode.
BatchEditRowDeletingEventFires on the client side before a data row is marked as deleted in batch edit mode.
BatchEditRowRecoveringEventFires on the client side before a data row is recovered in batch edit mode.
BatchEditStartEditingEventFires when a cell/row switches to batch edit mode and allows you to disable cell/row editing.
BatchEditEndEditingEventFires before a cell/row switches from edit to browse mode.
GetCellValue / GetCellValueByKeyMethodGets the value of the specified cell.
IsDeletedRow / IsDeletedRowByKeyMethodIndicates whether the specified row is marked as deleted in batch edit mode.
IsNewRowMethodIndicates whether the specified row is newly inserted.
SetCellValue / SetCellValueByKeyMethodAssigns a new value to the specified cell in batch edit mode.

View Example: How to implement clone functionality in batch edit mode

View Example: How to calculate values dynamically in batch edit mode

View Example: Cascading combo boxes in batch edit mode

View Example: How to use WebMethods to implement cascading combo boxes in batch edit mode

View Example: How to implement an edit item template in batch mode

Preview Changes

In batch edit mode, ASPxGridView keeps changes on callbacks when a user interacts with unsaved modified data (for instance, when a user navigates to another page within the grid or sorts data).

If the control’s KeepChangesOnCallbacks property is enabled, the grid displays the Preview changes button in the Status Bar. When a user clicks this button, the grid shows only modified rows from all its pages.

MemberTypeDescription
BatchEditChangesPreviewShowingEventFires before the grid switches to Preview Changes mode.
BatchEditChangesPreviewShownEventFires after the grid switches to Preview Changes mode.
ShowChangesPreviewMethodSwitches the grid to Preview Changes mode.
HideChangesPreviewMethodSwitches the grid from Preview Changes to browse mode.

Save and Discard Changes

When a user clicks the Save changes button or you call the control’s client-side UpdateEdit method, the grid raises the BatchEditChangesSaving event. Use the insertedValues, updatedValues, and deletedValues argument properties to obtain information about modified data.

aspx
<ClientSideEvents BatchEditChangesSaving="ChangesSaving" />
js
function ChangesSaving(s, e) {
    var updatedValues = e.updatedValues;
    var insertedValues = e.insertedValues;
    var deletedValues = e.deletedValues;
    // ...
}

Alternatively, you can call the GetUnsavedChanges method to get a hash table of inserted, updated, and deleted values.

js
var batchEditChanges = ClientGrid.batchEditApi.GetUnsavedChanges();

When a user clicks the Cancel changes button or you call the control’s client-side CancelEdit method, the grid raises the BatchEditChangesCanceling event.

MemberTypeDescription
BatchUpdateEventFires on the server side before the grid saves changes and allows you to implement custom updating rules.
BatchEditConfirmShowingEventFires before the grid shows the Confirmation dialog box.
BatchEditRowChangesCancelingEventFires when a user clicks a row’s Cancel button in batch edit mode.
HasChangesMethodIndicates whether the grid or the specified row/cell has unsaved changes in batch edit mode.
HasChangesByKey(key)MethodIndicates whether the specified row or cell has unsaved changes in batch edit mode.
ResetChanges(visibleIndex) / ResetChangesByKey(key)MethodResets changes in the specified cell.

View Example: A simple batch editing implementation

Validate Data

To validate data in batch edit mode, the grid raises the BatchEditRowValidating event in the following cases:

In a BatchEditRowValidating event handler, you can use the validationInfo argument property to get information about the modified row. This property also allows you to define whether the data is valid and specify an error text string for invalid data cells.

aspx
<ClientSideEvents BatchEditRowValidating="RowValidating"/>
js
function RowValidating(s, e) {
    for (var columnIndex in e.validationInfo) {
        var validationInfo = e.validationInfo[columnIndex];
        if (validationInfo.value === null) {
            validationInfo.isValid = false;
            validationInfo.errorText = "Invalid data";
        }
    }
}
MemberDescription
AllowValidationOnEndEditSpecifies whether to validate data when a cell/row/record switches from edit to browse mode.
AllowEndEditOnValidationErrorSpecifies whether an editor can lose focus when validation fails.
ErrorImagePositionGets or sets the position of the validation error image relative to the editor content.

View Example: How to implement custom date validation in batch edit mode

View Example: How to validate entered cell values on the server in batch edit mode

Implement a Data Item Template

In batch edit mode, ASPxGridView copies only HTML markup of the template and disables its client-side functionality. When a cell switches to edit mode, the grid shows an in-place editor to modify data. When the cell value changes and the cell switches to browse mode, the grid replaces the template content with the entered value.

You can change this behavior in the following ways:

Allow the grid to render the template content after cell editing ends

To enable this behavior, set the control’s AllowRegularDataItemTemplate property to true.

aspx
<dx:ASPxGridView ID="Grid" runat="server" ...">
    <Columns>
        <dx:GridViewDataTextColumn FieldName="TemplatedColumn" />
            <DataItemTemplate>
                <!-- ... -->
            </DataItemTemplate>
        </dx:GridViewDataTextColumn>
    </Columns>
    <SettingsEditing Mode="Batch">
        <BatchEditSettings AllowRegularDataItemTemplate="true" />
    </SettingsEditing>
</dx:ASPxGridView>

Disable cell editingIn this mode, the templated cell is non-editable but the client-side functionality of the template’s UI elements is available. For more information, refer to the following topic: Data Item Template.

To get a container object in batch edit mode, call the GetCellTextContainer(visibleIndex, columnFieldNameOrId) or GetCellTextContainerByKey(key, columnFieldNameOrId) method.

View Example: How to use and modify a control placed in DataItemTemplate

Multiple Cell Selection

In batch edit mode, ASPxGridView supports multiple cell selection.

Run Demo: ASPxGridView - Multiple Cell Selection

To enable this functionality, set the grid’s EnableMultipleCellSelection property to true.

aspx
<SettingsEditing Mode="Batch">
    <BatchEditSettings EnableMultipleCellSelection="true"/>
</SettingsEditing>

When the cell selection changes, the grid raises the CellSelectionChanging event. You can get the current selection state of the processed cell in a handler of this event and cancel the selection change.

Call the SelectCell(visibleIndex, rowIndex) or UnselectCell(visibleIndex, rowIndex) method to select or deselect a cell.

Customize the Grid Appearance and Layout

In batch edit mode, ASPxGridView highlights modified cells, as well as inserted and deleted rows.

The table below lists the properties that allow you to customize the grid appearance in batch edit mode.

MemberDescription
HighlightDeletedRowsSpecifies whether the grid highlights deleted rows.
BatchEditModifiedCellSpecifies the appearance of modified data cells.
BatchEditModifiedCellStyleSpecifies the appearance of the column’s modified data cells.
BatchEditNewRowSpecifies the appearance of inserted rows.
BatchEditDeletedRowSpecifies the appearance of deleted rows.
BatchEditChangesPreviewGroupRowSpecifies the appearance of group rows when the grid is in Preview Changes mode.
FocusedCellSpecifies the appearance of the focused data cell.

View Example: How to modify cell styles dynamically in Batch Edit mode

Display a Custom Toolbar

ASPxGridView allows you to control the visibility of the Status Bar where the grid displays the Preview changes , Save changes , and Cancel changes buttons. You can set the ShowStatusBar property to Hidden to hide the Status Bar and create a custom toolbar with command buttons. To display the toolbar in Preview Changes mode, enable the item’s VisibleInBatchEditPreviewChanges property.

aspx
<Toolbars>
    <dx:GridViewToolbar>
        <Items>
            <dx:GridViewToolbarItem Text="Edit commands" Command="Custom"
                VisibleInBatchEditPreviewChanges="true">
                <Items>
                    <dx:GridViewToolbarItem Command="PreviewChanges" />
                    <dx:GridViewToolbarItem Command="HidePreview" />
                    <dx:GridViewToolbarItem Command="Update" />
                    <dx:GridViewToolbarItem Command="Cancel" />
                </Items>
            </dx:GridViewToolbarItem>
        </Items>
    </dx:GridViewToolbar>
</Toolbars>
<Settings ShowStatusBar="Hidden" />

View Example: How to implement custom buttons in the status bar in batch edit mode

Limitations

In batch edit mode, ASPxGridView has the following limitations:

Unsupported features

  • Cell merge.
  • The date range functionality of embedded date editors when the grid’s EditMode property is set to Cell.
  • Tabular navigation through non-editable cells (for instance, group row cells or expand buttons).

Unsupported Server-Side API

Unsupported Client-Side API