Back to Devexpress

Command Columns

aspnet-3701-components-grid-view-concepts-data-representation-basics-columns-command-columns.md

latest5.1 KB
Original Source

Command Columns

  • Dec 23, 2024
  • 3 minutes to read

Users can use command columns to manipulate ASPxGridView data. ASPxGridView stores command columns together with its data columns within the ASPxGridView.Columns collection.

Commands

A command column is a GridViewCommandColumn class instance. This column supports commands that allow users to switch ASPxGridView to edit mode, update data, delete rows, and more.

A command column allows you to display multiple command items within a cell. Each command item is a single command. A command column supports the following eight command items:

CommandDescriptionProperty
NewCreates a new data row.ASPxGridViewCommandButtonSettings.NewButton
EditSwitches ASPxGridView to an edit mode.ASPxGridViewCommandButtonSettings.EditButton
DeleteDeletes the current data row.ASPxGridViewCommandButtonSettings.DeleteButton
SelectSelects/deselects data rows.ASPxGridViewCommandButtonSettings.SelectButton, GridViewCommandColumn.ShowSelectCheckbox
UpdateSaves all changes made to the current data row and switches ASPxGridView to a browse mode.ASPxGridViewCommandButtonSettings.UpdateButton
CancelDiscards any changes made to the current data row and switches ASPxGridView to a browse mode.ASPxGridViewCommandButtonSettings.CancelButton
ClearClears the filter expression applied to ASPxGridView.ASPxGridViewCommandButtonSettings.ClearFilterButton
RecoverRecovers a deleted data row.ASPxGridViewCommandButtonSettings.RecoverButton

A command item is displayed as a link (the default setting). The grid can also display this item as a button or image instead. Use the GridViewCommandColumn.ButtonRenderMode property to specify how the command column renders its command items.

Handle the ASPxGridView.CommandButtonInitialize event to initialize individual command buttons.

Behavior

Users can use a command column’s header to drag this column among visible columns or move it to the Customization Window. The GridViewCommandColumn.AllowDragDrop property enables the drag-and-drop functionality for the command column. If this property is set to Default , the ASPxGridViewBehaviorSettings.AllowDragDrop property defines the column behavior.

Custom Buttons

Command columns can also display custom buttons within command cells or the filter row. You can create your own buttons, and define custom actions for them. Refer to the following topic for an example: How to: Create Custom Command Buttons.

This example shows how to create and initialize a command column with custom command buttons at runtime.

csharp
using DevExpress.Web.ASPxGridView;

protected void Page_Load(object sender, EventArgs e) {
    if (!IsPostBack) {
        // Creates and initializes a command column.
        GridViewCommandColumn commandCol = new GridViewCommandColumn("Action");
        commandCol.Name = "Action";
        ASPxGridView1.Columns.Add(commandCol);
    }
    // Creates a custom command button.
    (ASPxGridView1.Columns["Action"] 
    as GridViewCommandColumn).CustomButtons.Add(CreateCustomButton());
}

GridViewCommandColumnCustomButton CreateCustomButton() {
    GridViewCommandColumnCustomButton customBtn = new GridViewCommandColumnCustomButton();
    customBtn.ID = "action1";
    customBtn.Text = "Action1";
    customBtn.Visibility = GridViewCustomButtonVisibility.BrowsableRow;
    return customBtn;
}
// Occurs after a command button has been clicked.
protected void ASPxGridView1_CustomButtonCallback(object sender,
ASPxGridViewCustomButtonCallbackEventArgs e) {
    if (e.ButtonID == "action1") {
        // ...
    }
}

Handle the ASPxGridView.CustomButtonInitialize event to initialize individual custom command buttons.