Example T401286
Visible to All Users

Grid View for ASP.NET MVC - Implement the Select All check box for a templated column in batch edit mode

This example demonstrates how to create a header template, add a check box to the template, and implement the Select All functionality in batch edit mode.

SelectAllCheckBox

Overview

Follow the steps below to implement the Select All functionality in a column's header in batch edit mode:

  1. Call a column's SetHeaderTemplateContent method and add a check box editor to the template.
    C#
    settings.Columns.Add(column => { column.FieldName = "IsReserved"; column.ColumnType = MVCxGridViewColumnType.CheckBox; column.EditorProperties().CheckBox(p => { p.ClientSideEvents.CheckedChanged = "OnCellCheckedChanged"; p.ValidationSettings.Display = Display.Dynamic; }); column.SetHeaderTemplateContent(c => { ViewContext.Writer.Write("<div style='text-align:center;'>"); Html.DevExpress().CheckBox(headerCheckBoxSettings => { headerCheckBoxSettings.Name = "HeaderCheckBox"; headerCheckBoxSettings.Properties.AllowGrayed = true; headerCheckBoxSettings.Properties.AllowGrayedByClick = false; headerCheckBoxSettings.Properties.ClientSideEvents.CheckedChanged = "OnHeaderCheckBoxCheckedChanged"; headerCheckBoxSettings.Properties.ClientSideEvents.Init = "OnInitHeader"; }).GetHtml(); ViewContext.Writer.Write("</div>"); }); });
  2. Handle the editor's client-side CheckedChanged event. In the handler, call the grid's SetCellValue method to assign a value to the specified cell based on a check box state.
    JavaScript
    function OnHeaderCheckBoxCheckedChanged(s, e) { var visibleIndices = Grid.batchEditApi.GetRowVisibleIndices(); var totalRowsCountOnPage = visibleIndices.length; for (var i = 0; i < totalRowsCountOnPage ; i++) { Grid.batchEditApi.SetCellValue(visibleIndices[i], "IsReserved", s.GetChecked()) } }
  3. Handle the grid's client-side BatchEditEndEditing, BatchEditRowDeleting, and BatchEditRowInserting events. In the handlers, call the CheckSelectedCellsOnPage function. In this function, compare the number of selected rows and the total number of visible rows. Based on a result, specify the state of the checkbox editor.
    JavaScript
    function CheckSelectedCellsOnPage(checkType) { var currentlySelectedRowsCount = 0; var visibleIndices = Grid.batchEditApi.GetRowVisibleIndices(); var totalRowsCountOnPage = visibleIndices.length; for (var i = 0; i < totalRowsCountOnPage ; i++) { if (Grid.batchEditApi.GetCellValue(visibleIndices[i], "IsReserved")) currentlySelectedRowsCount++; } if (checkType == "insertCheck") totalRowsCountOnPage++; else if (checkType == "deleteCheck") { totalRowsCountOnPage--; if (DeletedValue) currentlySelectedRowsCount--; } if (currentlySelectedRowsCount <= 0) HeaderCheckBox.SetCheckState("Unchecked"); else if (currentlySelectedRowsCount >= totalRowsCountOnPage) HeaderCheckBox.SetCheckState("Checked"); else HeaderCheckBox.SetCheckState("Indeterminate"); }

Files to Review

Documentation

More Examples

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

T401286/Views/Home/GridViewPartialView.cshtml
Razor
@{ var grid = Html.DevExpress().GridView(settings => { settings.Name = "Grid"; settings.CallbackRouteValues = new { Controller = "Home", Action = "GridViewPartialView" }; settings.SettingsEditing.BatchUpdateRouteValues = new { Controller = "Home", Action = "BatchUpdateRoomsPartial" }; settings.SettingsEditing.Mode = GridViewEditingMode.Batch; settings.SettingsPager.PageSize = 5; settings.SettingsBehavior.AllowSort = false; settings.ClientSideEvents.BatchEditStartEditing = "OnBatchEditStartEditing"; settings.ClientSideEvents.BatchEditRowDeleting = "OnBatchEditRowDeleting"; settings.ClientSideEvents.BatchEditRowInserting = "OnBatchEditRowInserting"; settings.CommandColumn.Visible = true; settings.CommandColumn.ShowDeleteButton = true; settings.CommandColumn.ShowNewButtonInHeader = true; settings.Width = 400; settings.KeyFieldName = "ID"; settings.Columns.Add("Number"); settings.Columns.Add("Floor"); settings.Columns.Add(column => { column.FieldName = "IsReserved"; column.ColumnType = MVCxGridViewColumnType.CheckBox; column.EditorProperties().CheckBox(p => { p.ClientSideEvents.CheckedChanged = "OnCellCheckedChanged"; p.ValidationSettings.Display = Display.Dynamic; }); column.SetHeaderTemplateContent(c => { ViewContext.Writer.Write("<div style='text-align:center;'>"); Html.DevExpress().CheckBox(headerCheckBoxSettings => { headerCheckBoxSettings.Name = "HeaderCheckBox"; headerCheckBoxSettings.Properties.AllowGrayed = true; headerCheckBoxSettings.Properties.AllowGrayedByClick = false; headerCheckBoxSettings.Properties.ClientSideEvents.CheckedChanged = "OnHeaderCheckBoxCheckedChanged"; headerCheckBoxSettings.Properties.ClientSideEvents.Init = "OnInitHeader"; }).GetHtml(); ViewContext.Writer.Write("</div>"); }); }); }); } @grid.Bind(Model).GetHtml()
T401286/Views/Home/Index.cshtml
Razor
<script type="text/javascript"> var visibleIndex; var DeletedValue; function OnInitHeader(s, e) { setTimeout(function () { CheckSelectedCellsOnPage("usualCheck"); }, 0); } function OnHeaderCheckBoxCheckedChanged(s, e) { var visibleIndices = Grid.batchEditApi.GetRowVisibleIndices(); var totalRowsCountOnPage = visibleIndices.length; for (var i = 0; i < totalRowsCountOnPage ; i++) { Grid.batchEditApi.SetCellValue(visibleIndices[i], "IsReserved", s.GetChecked()) } } function OnCellCheckedChanged(s, e) { Grid.batchEditApi.SetCellValue(visibleIndex, "IsReserved", s.GetValue()); CheckSelectedCellsOnPage("usualCheck"); } function OnBatchEditRowDeleting(s, e) { DeletedValue = Grid.batchEditApi.GetCellValue(e.visibleIndex, "IsReserved"); CheckSelectedCellsOnPage("deleteCheck"); } function OnBatchEditRowInserting(s, e) { CheckSelectedCellsOnPage("insertCheck"); } function OnBatchEditStartEditing(s, e) { visibleIndex = e.visibleIndex; } function CheckSelectedCellsOnPage(checkType) { var currentlySelectedRowsCount = 0; var visibleIndices = Grid.batchEditApi.GetRowVisibleIndices(); var totalRowsCountOnPage = visibleIndices.length; for (var i = 0; i < totalRowsCountOnPage ; i++) { if (Grid.batchEditApi.GetCellValue(visibleIndices[i], "IsReserved")) currentlySelectedRowsCount++; } if (checkType == "insertCheck") totalRowsCountOnPage++; else if (checkType == "deleteCheck") { totalRowsCountOnPage--; if (DeletedValue) currentlySelectedRowsCount--; } if (currentlySelectedRowsCount <= 0) HeaderCheckBox.SetCheckState("Unchecked"); else if (currentlySelectedRowsCount >= totalRowsCountOnPage) HeaderCheckBox.SetCheckState("Checked"); else HeaderCheckBox.SetCheckState("Indeterminate"); } </script> @using (Html.BeginForm()) { @Html.Action("GridViewPartialView") }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.