Selection

  • 5 min to read

This topic describes the concept of row selection, including an overview of the selection functionality, as well as how to process selected records on client and server sides.

Selection Overview

The GridView allows end-users to select rows using the following methods.

If the ASPxGridViewBehaviorSettings.AllowSelectByRowClick option (using GridViewSettings.SettingsBehavior.AllowSelectByRowClick) is enabled, end-users can change the selection by clicking rows in combination with the SHIFT or CTRL key. In multiple row selection mode, clicking a row while holding down the CTRL key toggles the row's selected state. Contiguous rows can be selected by clicking the first row and the last row while holding down the SHIFT key. After this, the selection can be further customized by clicking individual rows while holding down the CTRL key.

The GridView supports single and multiple row selection modes. To switch to single row selection mode, set the ASPxGridViewBehaviorSettings.AllowSelectSingleRowOnly property value (using GridViewSettings.SettingsBehavior.AllowSelectSingleRowOnly) to true. In single row selection mode, the command column cells display radio buttons instead of check boxes.

cdSelectionOverview

The style settings used to paint selected rows can be accessed and customized using the GridViewStyles.SelectedRow property (using GridViewSettings.Styles.SelectedRow).

Processing Selected Rows on the Client Side

The GridView provides a full-featured client-side API that allows you to select and deselect rows using JS code. The table below lists the available selection-related methods.

Method Description
ASPxClientGridView.GetSelectedFieldValues (using MVCxClientGridView.GetSelectedFieldValues) Returns the row values displayed within all selected rows.
ASPxClientGridView.GetSelectedKeysOnPage (using MVCxClientGridView.GetSelectedKeysOnPage) Returns key values of selected rows displayed within the current page.
ASPxClientGridView.GetSelectedRowCount (using MVCxClientGridView.GetSelectedRowCount) Returns the number of selected rows.
ASPxClientGridView.IsRowSelectedOnPage (using MVCxClientGridView.IsRowSelectedOnPage) Indicates whether or not the specified row is selected within the current page.
ASPxClientGridView.SelectAllRowsOnPage (using MVCxClientGridView.SelectAllRowsOnPage) Allows you to select or deselect all rows displayed on the current page based on the parameter passed.
ASPxClientGridView.SelectRowOnPage (using MVCxClientGridView.SelectRowOnPage) Selects or deselects the specified row displayed on the current page.
ASPxClientGridView.SelectRows (using MVCxClientGridView.SelectRows) Selects or deselects the specified rows within the grid.
ASPxClientGridView.SelectRowsByKey (using MVCxClientGridView.SelectRowsByKey) Selects or deselects the specified row in the grid.
ASPxClientGridView.UnselectAllRowsOnPage (using MVCxClientGridView.UnselectAllRowsOnPage) Deselects all selected rows displayed on the current page.
ASPxClientGridView.UnselectFilteredRows (using MVCxClientGridView.UnselectFilteredRows) Deselects all grid rows that match the filter criteria currently applied to the grid.
ASPxClientGridView.UnselectRowOnPage (using MVCxClientGridView.UnselectRowOnPage) Deselects the specified row (if selected) displayed on the current page.
ASPxClientGridView.UnselectRows (using MVCxClientGridView.UnselectRows) Deselects the specified row (if selected) within the grid.
ASPxClientGridView.UnselectRowsByKey (using MVCxClientGridView.UnselectRowsByKey) Deselects the specified row displayed within the grid.

Example

In this example, the ASPxClientGridView.SelectionChanged client-side event (using MVCxClientGridView.SelectionChanged) is handled to display selected contacts within a list box. Contact names are obtained using the ASPxClientGridView.GetSelectedFieldValues function (using MVCxClientGridView.GetSelectedFieldValues).

View code:


<script type="text/javascript">
    //<![CDATA[
    function SelectionChanged(s, e) {
        s.GetSelectedFieldValues("ContactName", GetSelectedFieldValuesCallback);
    }
    function GetSelectedFieldValuesCallback(values) {
        SelectedRows.BeginUpdate();
        SelectedRows.ClearItems();
        for (var i = 0; i < values.length; i++) {
            SelectedRows.AddItem(values[i]);
        }
        SelectedRows.EndUpdate();
    }
    // ]]>
</script>

<div style="float:left;">
    <div style="float: left; margin: 5px;">
        <div class="text">
            Selected values:
        </div>
        @Html.DevExpress().ListBox(settings => {
            settings.Name = "SelectedRows";
            settings.Properties.EnableClientSideAPI = true;
        }).GetHtml()
    </div>
    @Html.Action("GridViewPartial")
</div>

Partial View code:


@Html.DevExpress().GridView(settings => {
    settings.Name = "GridView";
    settings.CallbackRouteValues = new { Controller = "Home", Action = "GridViewPartial" };
    settings.KeyFieldName = "CustomerID";
    settings.Columns.Add("ContactName");
    settings.Columns.Add("CompanyName");
    settings.Columns.Add("City");
    settings.Columns.Add("Region");
    settings.Columns.Add("Country"); 

    settings.CommandColumn.ShowSelectCheckbox = true;
    settings.CommandColumn.Visible = true;
    settings.ClientSideEvents.SelectionChanged = "SelectionChanged";
}).Bind(Model).GetHtml()

The image below illustrates the result.

MVC_Grid_FN_Selection_Select2ListBox

Processing Selected Rows on the Server Side

To obtain the selected rows on the server side, get the selected rows on the client-side and then send them to the server using a callback.

Example

In this example, the ASPxClientGridView.SelectionChanged client-side event (using MVCxClientGridView.SelectionChanged) is handled to get a collection of selected values and send them to the server side with a callback. Contact names and cities are obtained using the ASPxClientGridView.GetSelectedFieldValues function (using MVCxClientGridView.GetSelectedFieldValues), and are passed to the OnGetValues function. The OnGetValues function concatenates the selected values to a single string and sends the resulting string to the server using the MVCxClientGridView.PerformCallback method. In the controller code, you can split the string into separate values.

View code:


<script type="text/javascript">
    function OnGetValues(values) {
        var userSelect = "";
        for (var i in values) {
            userSelect = userSelect + values[i] + ";";
        }
        // The "PerformCallback" method passes the string with the selected values 
        // to a server as a Request.Params["selectedValues"] item.
        GridView.PerformCallback({ selectedValues: userSelect });
    }
</script>

@Html.Action("GridViewPartial")

Partial View code:


@Html.DevExpress().GridView(settings => {
    settings.Name = "GridView";
    settings.CallbackRouteValues = new { Controller = "Home", Action = "GridViewPartial" };
    settings.CommandColumn.ShowSelectCheckbox = true;
    settings.CommandColumn.Visible = true;

    settings.KeyFieldName = "CustomerID";
    settings.Columns.Add("ContactName");
    settings.Columns.Add("CompanyName");
    settings.Columns.Add("City");
    settings.Columns.Add("Region");
    settings.Columns.Add("Country");
    // When an end-user selects grid records, the values of the "ContactName" and "City" fields of the selected records are passed to the "OnGetValues" function.
    settings.ClientSideEvents.SelectionChanged = "function(s,e){ s.GetSelectedFieldValues('ContactName;City', OnGetValues); }";
}).Bind(Model).GetHtml()

Controller code:


using System.Web.Mvc;
using System.Linq;

namespace MyProject.Controllers {
    public class HomeController : Controller {
        // ...
        public ActionResult GridViewPartial() {
            // Contains all selected values, concatenated to a single string.
            var x = Request.Params["selectedValues"]; // "Maria Anders,Berlin;Thomas Hardy,London;" 

            var model = db.Customers;
            return PartialView("_GridViewPartial", model.ToList());
        }
    }
}