Grid View for ASP.NET Web Forms - 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.
Follow the steps below to implement the Select All functionality in a column's header in batch edit mode:
-
Specify a column's HeaderTemplate property and add a check box editor to the template.
<dx:GridViewDataCheckColumn FieldName="Discontinued" VisibleIndex="6"> <!-- ... --> <HeaderTemplate> <dx:ASPxCheckBox ID="HeaderCheckBox" ClientIDMode="Static" runat="server" ClientInstanceName="HeaderCheckBox" ... > <ClientSideEvents CheckedChanged="OnHeaderCheckBoxCheckedChanged" Init="OnInitHeader" /> </dx:ASPxCheckBox> </HeaderTemplate> <Settings AllowSort="False" /> </dx:GridViewDataCheckColumn>
-
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 the check box state.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], "Discontinued", s.GetChecked()) } }
-
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.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], "Discontinued")) 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"); }
- Default.aspx (VB: Default.aspx)
(you will be redirected to DevExpress.com to submit your response)