Grid View for ASP.NET MVC - How to select/deselect all rows in a group when data is grouped by one column
This example demonstrates how to allow users to add/remove all rows in a group to/from selection. Note that this technique works only when grid data is grouped by one column.
Call the grid's SetGroupRowContentTemplateContent method and place a check box and a label to the group row content template. Implement two-way data binding between the label's Text
property and group row markup.
settings.SetGroupRowContentTemplateContent(c => {
ViewContext.Writer.Write("<table><tr><td>");
Html.DevExpress().CheckBox(checkBox => {
checkBox.Name = "checkBox" + c.VisibleIndex.ToString();
<!-- ... -->
}).GetHtml();
ViewContext.Writer.Write("</td><td>");
Html.DevExpress().Label(label => {
label.Name = "label" + c.VisibleIndex.ToString();
label.Text = GetCaptionText(c);
}).GetHtml();
ViewContext.Writer.Write("</td></tr></table>");
});
protected string GetCaptionText(GridViewGroupRowTemplateContainer container) {
string captionText = !string.IsNullOrEmpty(container.Column.Caption) ? container.Column.Caption : container.Column.FieldName;
return string.Format("{0} : {1} {2}", captionText, container.GroupText, container.SummaryText);
}
Set the check box's CheckedChanged event to a function that sends the visible index of the group row and the check state of the check box as callback parameters to the server.
Html.DevExpress().CheckBox(checkBox => {
checkBox.Name = "checkBox" + c.VisibleIndex.ToString();
checkBox.Properties.ClientSideEvents.CheckedChanged = string.Format("function(s, e){{ {0}.PerformCallback({{parameters: '{1};' + s.GetCheckState()}}); }}", settings.Name, c.VisibleIndex);
checkBox.Init += (s, e) => {
var checkEditor = (MVCxCheckBox)s;
var checkState = GetCheckState(c);
checkEditor.AllowGrayed = true;
checkEditor.CheckState = GetCheckState(c);
};
}).GetHtml();
protected CheckState GetCheckState(GridViewGroupRowTemplateContainer container) {
var grid = (MVCxGridView)container.Grid;
var groupRowKey = GetGroupRowKey(grid, container.VisibleIndex);
var groupKeysCache = GetGroupKeysCache(grid);
var dataRowsKeys = groupKeysCache[groupRowKey];
var selectedCount = dataRowsKeys.Count(key => grid.Selection.IsRowSelectedByKey(key));
if (selectedCount == 0)
return CheckState.Unchecked;
if (dataRowsKeys.Count == selectedCount)
return CheckState.Checked;
return CheckState.Indeterminate;
}
Handle the grid's BeforeGetCallbackResult event to process the callback. Pass the data row's key and the check state of the checkbox to the SetSelectionByKey method to select or deselect this row.
settings.BeforeGetCallbackResult += (sender, e) => {
MVCxGridView gridView = (MVCxGridView)sender;
if (Session["Grouped"] != null && Session["Grouped"].ToString() == "clear") {
if (ViewBag.ClearGrouping == true) {
foreach (var columns in gridView.DataColumns) {
columns.UnGroup();
}
Session["Grouped"] = "";
}
return;
}
if (ViewData["data"] != null) {
string[] parameters = ViewData["data"].ToString().Split(';');
int visibleIndex = int.Parse(parameters[0]);
bool isGroupRowSelected = false;
CheckState currentState = (CheckState)Enum.Parse(typeof(CheckState), parameters[1]);
switch(currentState) {
case CheckState.Indeterminate:
case CheckState.Checked:
isGroupRowSelected = true;
break;
case CheckState.Unchecked:
isGroupRowSelected = false;
break;
}
var groupRowKey = GetGroupRowKey(gridView, visibleIndex);
var groupKeysCache = GetGroupKeysCache(gridView);
var dataRowsKeys = groupKeysCache[groupRowKey];
foreach (var dataRowKey in dataRowsKeys) {
gridView.Selection.SetSelectionByKey(dataRowKey, isGroupRowSelected);
}
}
if(ViewBag.GroupedColumns == null)
return;
string[] columnNames = ViewBag.GroupedColumns.Split(';');
gridView.ClearSort();
foreach(string name in columnNames) {
gridView.GroupBy(gridView.Columns[name]);
}
Session["Grouped"] = "";
gridView.CollapseAll();
};