Skip to main content
All docs
V18.2

Adding and Deleting Records

  • 3 minutes to read

Creating and Initializing New Rows

End-users can create new rows via the New command.

To allow end-users to add new rows, create a command column, access the New command’s settings via the ASPxGridViewCommandButtonSettings.NewButton property and set the GridViewCommandColumnButton.Visible property to true.

cdCreatingNewRow

To create rows in code, use the server ASPxGridView.AddNewRow or client ASPxClientGridView.AddNewRow method. These methods switch ASPxGridView to an edit mode and allow the values of the new row to be edited.

To initialize row values in code, handle the ASPxGridView.InitNewRow event.

Example:

In this example the ASPxGridView.InitNewRow event is handled to initialize the new record’s field values with default values.

The image below shows the result:

exInitNewRow

protected void grid_InitNewRow(object sender, DevExpress.Web.Data.ASPxDataInitNewRowEventArgs e) {
    e.NewValues["BirthDate"] = new DateTime(1970, 1, 10);
    e.NewValues["Title"] = "Sales Representative";
}

Adding Rows

To add a new record to the underlying data source, call the ASPxGridView.UpdateEdit (or ASPxClientGridView.UpdateEdit) method. End-users can do this by clicking the Update command.

cdAddingNewRow

You can also implement row validation. For information, see Row Validation and Error Indication.

After a new row has been added to ASPxGridView, the ASPxGridView.RowInserted event is raised. To cancel the insert operation, handle the ASPxGridView.RowInserting event.

Deleting Rows

End-users can delete rows via the Delete command. To do this in code, use the server ASPxGridView.DeleteRow or client ASPxClientGridView.DeleteRow method.

After a row has been deleted, the ASPxGridView.RowDeleted event is raised. To cancel the delete operation, handle the ASPxGridView.RowDeleting event.

To allow an end-user to cancel the delete operation, enable the ASPxGridBehaviorSettings.ConfirmDelete option. In this case, when the user deletes a row, the Confirm Delete window is automatically displayed.

ConfirmDelete

The text displayed within the Confirm Delete window can be specified via the ASPxGridTextSettings.ConfirmDelete property.

 

Example:

The example illustrates how to delete selected rows of the ASPxGridView bound with an in-memory DataSource.See Also:How to move selected rows from the ASPxGridView into another ASPxGridViewEditing an in-memory dataset

protected void gridView_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e) {
    if(e.Parameters == "Delete") {
        table = GetTable();
        List<Object> selectItems = grid.GetSelectedFieldValues("ID");
        foreach(object selectItemId in selectItems) {
            table.Rows.Remove(table.Rows.Find(selectItemId));
        }
        grid.DataBind();
        grid.Selection.UnselectAll();
    }
}