Skip to main content
A newer version of this page is available. .
All docs
V21.1

Data Grid Command API

  • 3 minutes to read

You can maintain a clean MVVM pattern and define custom logic in a ViewModel. The GridControl includes the following command counterparts for events that expect a return value:

Property Description
GridControl.CustomColumnDisplayTextCommand, TreeListView.CustomColumnDisplayTextCommand Gets or sets a command that customizes a data cell‘s display text.
CustomGroupDisplayTextCommand Gets or sets a command that displays custom text in group rows.
CustomColumnGroupCommand Gets or sets a command that applies custom rules to group rows.
GridControl.CustomColumnSortCommand, TreeListView.CustomColumnSortCommand Gets or sets a command that uses custom rules to sort rows.
CustomUniqueValuesCommand Gets or sets a command that populates a column’s Drop-down Filter with unique values.
CustomRowFilterCommand Gets or sets a command that uses custom rules to filter rows.
GridControl.CustomSummaryCommand, TreeListView.CustomSummaryCommand Gets or sets a command that calculates a custom summary.
CustomSummaryExistsCommand Gets or sets a command that specifies which summaries should be calculated and displayed.
AddingNewRowCommand Gets or sets a command that is executed before a new row is added to the GridControl.
GridViewBase.RowCanceledCommand, TreeListView.NodeCanceledCommand Gets or sets a command that is executed when the changes made in a row are discarded.
GridViewBase.RowUpdatedCommand, TreeListView.NodeUpdatedCommand Gets or sets a command executed when the GridControl updates the data source with the changes made within the focused row.
TableView.RowDoubleClickCommand, TreeListView.RowDoubleClickCommand Gets or sets a command executed when a user double-clicks a row.
GridViewBase.ValidateRowCommand, TreeListView.ValidateNodeCommand Gets or sets a command that validates row values.

These commands expose UI-independent arguments and pass them to the ViewModel as a typed parameter. As a result, you can modify the command parameter at the ViewModel level and return values to a View:

<dxg:GridControl CustomColumnDisplayTextCommand="{Binding CalculateDisplayTextCommand}" /> 
public class ViewModel: ViewModelBase {
    // ...

    [Command]
    public void CalculateDisplayText(DevExpress.Mvvm.Xpf.CustomColumnDisplayTextArgs args) {
        args.DisplayText = GetLocalizedValue(args.FieldName, args.Value, Language);
    }
}

If an event does not have a command counterpart property, you can use the EventToCommand behavior to bind your command to the event. This behavior allows you to pass a parameter to the bound command and return the parameter to the event.

Built-in Commands

The GridControl includes multiple built-in commands that allow you to expand/collapse group rows, move focus, sort/group data, and so on.

The following classes store these commands:

Class Description
GridCommands Provides access to built-in grid commands.
DataViewCommandsBase Contains built-in commands common to all the GridControl views.
TableViewCommands Provides access to the TableView‘s built-in commands.
TreeListViewCommands Provides access to the TreeListView‘s built-in commands.
CardViewCommands Provides access to the CardView‘s built-in commands.

Refer to these classes’ members to get the full list of available commands.

The code snippet below demonstrates how to use built-in commands:

<Button Command="{Binding ElementName=grid,Path=View.Commands.ShowSearchPanel}" />

Custom Commands

You can specify your own commands in a ViewModel. The following help topic illustrates how to create a command that deletes selected rows from the GridControl: Binding to a Collection of Selected Items.

DevExpress WPF | Grid Control - Delete Selected Rows Command