Back to Devexpress

Data Grid Command API

wpf-403148-controls-and-libraries-data-grid-mvvm-enhancements-data-grid-command-api.md

latest9.1 KB
Original Source

Data Grid Command API

  • Aug 25, 2023
  • 4 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:

PropertyDescription
GridViewBase.AddingNewRowCommand, TreeListView.AddingNewNodeCommandGets or sets a command that is executed before a new row is added to the GridControl.
TableView.CellMergeCommandGets or sets a command that allows you to specify custom cell merge rules.
GridViewBase.CellValueChangedCommand, TreeListView.CellValueChangedCommandGets or sets a command executed when a cell value is changed.
GridViewBase.CellValueChangingCommand, TreeListView.CellValueChangingCommandGets or sets a command executed when a user edits a cell value (for example, types or deletes a character, chooses a value from the drop-down list).
GridControl.CustomColumnDisplayTextCommand, TreeListView.CustomColumnDisplayTextCommandGets or sets a command that customizes a data cell‘s display text.
CustomColumnGroupCommandGets or sets a command that applies custom rules to group rows.
GridControl.CustomColumnSortCommand, TreeListView.CustomColumnSortCommandGets or sets a command that uses custom rules to sort rows.
CustomGroupDisplayTextCommandGets or sets a command that displays custom text in group rows.
GridControl.CustomRowFilterCommand, TreeListView.CustomNodeFilterCommandGets or sets a command that uses custom rules to filter rows.
GridControl.CustomSummaryCommand, TreeListView.CustomSummaryCommandGets or sets a command that calculates a custom summary.
CustomSummaryExistsCommandGets or sets a command that specifies which summaries should be calculated and displayed.
GridControl.CustomUnboundColumnDataCommand, TreeListView.CustomUnboundColumnDataCommandGets or sets a command that populates unbound columns with data.
CustomUniqueValuesCommandGets or sets a command that populates a column’s Drop-down Filter with unique values.
TableView.InitNewRowCommand, TreeListView.InitNewNodeCommandGets or sets a command that allows you to initialize a new row with default values.
GridViewBase.InvalidRowExceptionCommand, TreeListView.InvalidNodeExceptionCommandGets or sets a command that is executed when a row fails validation or cannot be saved to a data source.
NodeChangedCommandGets or sets a command that is executed when a node’s property changes.
GridViewBase.RowCanceledCommand, TreeListView.NodeCanceledCommandGets or sets a command that is executed when the changes made in a row are discarded.
TableView.RowDoubleClickCommand, TreeListView.RowDoubleClickCommandGets or sets a command executed when a user double-clicks a row.
GridViewBase.RowUpdatedCommand, TreeListView.NodeUpdatedCommandGets or sets a command executed when the GridControl updates the data source with the changes made within the focused row.
GridViewBase.ValidateCellCommand, TreeListView.ValidateCellCommandGets or sets a command that allows you to validate the focused cell’s data.
GridViewBase.ValidateRowCommand, TreeListView.ValidateNodeCommandGets 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:

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

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

    <Command>
    Public Sub CalculateDisplayText(ByVal args As DevExpress.Mvvm.Xpf.CustomColumnDisplayTextArgs)
        args.DisplayText = GetLocalizedValue(args.FieldName, args.Value, Language)
    End Sub 
End Class

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.

Note

Command counterparts for events do not support Asynchronous Commands.

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:

ClassDescription
GridCommandsProvides access to built-in grid commands.
DataViewCommandsBaseContains built-in commands common to all the GridControl views.
TableViewCommandsProvides access to the TableView‘s built-in commands.
TreeListViewCommandsProvides access to the TreeListView‘s built-in commands.
CardViewCommandsProvides access to the CardView‘s built-in commands.

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

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

xaml
<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.