Skip to main content
Tab

ASPxGridView.CommandButtonInitialize Event

Enables individual command buttons to be initialized.

Namespace: DevExpress.Web

Assembly: DevExpress.Web.v24.2.dll

Declaration

public event ASPxGridViewCommandButtonEventHandler CommandButtonInitialize

Event Data

The CommandButtonInitialize event's data class is ASPxGridViewCommandButtonEventArgs. The following properties provide information specific to this event:

Property Description
ButtonType Gets the type of the command button currently being initialized.
Column Gets a command column which owns the processed command button.
Enabled Gets or sets whether the processed command button is enabled. Inherited from ASPxGridCommandButtonEventArgs.
Image Gets the settings of an image displayed within the processed command button. Inherited from ASPxGridCommandButtonEventArgs.
IsEditingRow Gets whether a command button is displayed within the data row currently being edited.
RenderMode Specifies the processed command button’s render mode. Inherited from ASPxGridCommandButtonEventArgs.
Styles Gets the processed command button’s style. Inherited from ASPxGridCommandButtonEventArgs.
Text Gets the processed command button’s text. Inherited from ASPxGridCommandButtonEventArgs.
Visible Gets or sets whether the command button is visible. Inherited from ASPxGridCommandButtonEventArgs.
VisibleIndex Gets the visible index of a data item (row, card or record) which contains the processed command button. Inherited from ASPxGridCommandButtonEventArgs.

Remarks

The CommandButtonInitialize event is raised for each built-in command button (edit, new, delete, etc.), and allows their settings to be initialized. For instance, you can hide or disable individual command buttons.

The processed button type is returned by the ASPxGridViewCommandButtonEventArgs.ButtonType property. The row in which this button is displayed can be identified by its visible index, returned by the ASPxGridCommandButtonEventArgs.VisibleIndex property. To specify whether the button should be visible or enabled, use the ASPxGridCommandButtonEventArgs.Visible and ASPxGridCommandButtonEventArgs.Enabled properties, respectively.

To initialize custom command buttons, handle the ASPxGridView.CustomButtonInitialize event.

Note

When Batch Edit Mode is enabled, the grid renders service rows with negative indices that are used to add new rows. These rows are hidden from users and are necessary for the correct operation of these features. In this case, the ASPxGridCommandButtonEventArgs.VisibleIndex parameter may have the -2147483647 value in the CommandButtonInitialize event handler when a new row is being edited.

To process only the command buttons of the visible data rows, use the following code in the CommandButtonInitialize event handler:

if(e.VisibleIndex >= 0){
 //custom actions
}

Online Example

View Example: How to specify the settings of built-in and custom command buttons based on a condition

View Example: How to update multiple grid controls on a custom button click

Example

This example demonstrates how to hide a command button in the grid’s CommandColumn by handling the CommandButtonInitialize event.

See Also:

View Example

namespace HideCommandButtons {
    public partial class _Default : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
            ASPxGridView1.DataSource = GetData();
            ASPxGridView1.DataBind();
        }

        private DataTable GetData() {
            DataTable table = new DataTable();
            table.Columns.Add("ID", typeof(int));
            for(int i = 0; i < 10; i++) {
                table.Rows.Add(i);
            }
            return table;
        }

        protected void ASPxGridView1_CommandButtonInitialize(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCommandButtonEventArgs e) {
            bool isOddRow = e.VisibleIndex % 2 == 0;
            if(isOddRow) {  // some condition
                // hide the Edit button
                if(e.ButtonType == DevExpress.Web.ASPxGridView.ColumnCommandButtonType.Edit)
                    e.Visible = false;

                // disable the selction checkbox
                if(e.ButtonType == DevExpress.Web.ASPxGridView.ColumnCommandButtonType.SelectCheckbox)
                    e.Enabled = false;
            }
        }
    }
}
See Also