ASPxGridView.CollapseAll() Method
Namespace: DevExpress.Web
Assembly: DevExpress.Web.v18.2.dll
Declaration
public void CollapseAll()
To collapse the specified group row, use the ASPxGridView.CollapseRow method. To expand all group rows, use the ASPxGridView.ExpandAll method.
Example
This example shows how to expand various groups of rows in ASPxGridView. Information about a row’s state is saved by clicking the ‘Save’ button. To restore saved rows’ state, click the ‘Restore’ button.
Visible indexes of the stored groups are shown in the text field below.
MVC version: T360561 - GridView - How to restore the expanded group rows state after a page is refreshed
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
public partial class _Default : System.Web.UI.Page
{
protected void btnSave_Click(object sender, EventArgs e) {
List<int> states = new List<int>();
for (Int32 i = 0; i < grid.VisibleRowCount; i++) {
if (grid.IsGroupRow(i) && grid.IsRowExpanded(i))
states.Add(i);
}
Session["expandedRows"] = states;
}
protected void btnLoad_Click(object sender, EventArgs e) {
List<int> states = Session["expandedRows"] as List<int> ;
if (states == null)
return;
grid.CollapseAll();
foreach (int index in states)
grid.ExpandRow(index);
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="DevExpress.Web.ASPxEditors.v10.2, Version=10.2.5.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web.ASPxEditors" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.ASPxGridView.v10.2, Version=10.2.5.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web.ASPxGridView" TagPrefix="dx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>How to restore the expanded group rows state after a page is refreshed</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<dx:ASPxGridView ID="grid" runat="server" AutoGenerateColumns="False" DataSourceID="sds"
KeyFieldName="OrderID" EnableRowsCache="False">
<Columns>
<dx:GridViewDataTextColumn FieldName="OrderID" ReadOnly="True" VisibleIndex="0">
<EditFormSettings Visible="False" />
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="CustomerID" GroupIndex="0" SortIndex="0" SortOrder="Ascending"
VisibleIndex="1">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="EmployeeID" GroupIndex="1" SortIndex="1" SortOrder="Ascending"
VisibleIndex="2">
</dx:GridViewDataTextColumn>
<dx:GridViewDataDateColumn FieldName="OrderDate" VisibleIndex="3">
</dx:GridViewDataDateColumn>
<dx:GridViewDataDateColumn FieldName="RequiredDate" VisibleIndex="4">
</dx:GridViewDataDateColumn>
<dx:GridViewDataDateColumn FieldName="ShippedDate" VisibleIndex="5">
</dx:GridViewDataDateColumn>
</Columns>
</dx:ASPxGridView>
<asp:SqlDataSource ID="sds" runat="server" ConnectionString="Data Source=(local);Initial Catalog=Northwind;Integrated Security=True"
ProviderName="System.Data.SqlClient" SelectCommand="SELECT [OrderID], [CustomerID], [EmployeeID], [OrderDate], [RequiredDate], [ShippedDate] FROM [Orders]">
</asp:SqlDataSource>
<table>
<tr>
<td>
<dx:ASPxButton ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click">
</dx:ASPxButton>
</td>
<td>
<dx:ASPxButton ID="btnLoad" runat="server" Text="Restore" OnClick="btnLoad_Click">
</dx:ASPxButton>
</td>
</tr>
</div>
</form>
</body>
</html>
Imports Microsoft.VisualBasic
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Collections.Generic
Partial Public Class _Default
Inherits System.Web.UI.Page
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim states As List(Of Integer) = New List(Of Integer)()
For i As Int32 = 0 To grid.VisibleRowCount - 1
If grid.IsGroupRow(i) AndAlso grid.IsRowExpanded(i) Then
states.Add(i)
End If
Next i
Session("expandedRows") = states
End Sub
Protected Sub btnLoad_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim states As List(Of Integer) = TryCast(Session("expandedRows"), List(Of Integer))
If states Is Nothing Then
Return
End If
grid.CollapseAll()
For Each index As Integer In states
grid.ExpandRow(index)
Next index
End Sub
End Class
<%@ Page Language="vb" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<%@ Register Assembly="DevExpress.Web.ASPxEditors.v10.2, Version=10.2.5.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web.ASPxEditors" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.ASPxGridView.v10.2, Version=10.2.5.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web.ASPxGridView" TagPrefix="dx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>How to restore the expanded group rows state after a page is refreshed</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<dx:ASPxGridView ID="grid" runat="server" AutoGenerateColumns="False" DataSourceID="sds"
KeyFieldName="OrderID" EnableRowsCache="False">
<Columns>
<dx:GridViewDataTextColumn FieldName="OrderID" ReadOnly="True" VisibleIndex="0">
<EditFormSettings Visible="False" />
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="CustomerID" GroupIndex="0" SortIndex="0" SortOrder="Ascending"
VisibleIndex="1">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="EmployeeID" GroupIndex="1" SortIndex="1" SortOrder="Ascending"
VisibleIndex="2">
</dx:GridViewDataTextColumn>
<dx:GridViewDataDateColumn FieldName="OrderDate" VisibleIndex="3">
</dx:GridViewDataDateColumn>
<dx:GridViewDataDateColumn FieldName="RequiredDate" VisibleIndex="4">
</dx:GridViewDataDateColumn>
<dx:GridViewDataDateColumn FieldName="ShippedDate" VisibleIndex="5">
</dx:GridViewDataDateColumn>
</Columns>
</dx:ASPxGridView>
<asp:SqlDataSource ID="sds" runat="server" ConnectionString="Data Source=(local);Initial Catalog=Northwind;Integrated Security=True"
ProviderName="System.Data.SqlClient" SelectCommand="SELECT [OrderID], [CustomerID], [EmployeeID], [OrderDate], [RequiredDate], [ShippedDate] FROM [Orders]">
</asp:SqlDataSource>
<table>
<tr>
<td>
<dx:ASPxButton ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click">
</dx:ASPxButton>
</td>
<td>
<dx:ASPxButton ID="btnLoad" runat="server" Text="Restore" OnClick="btnLoad_Click">
</dx:ASPxButton>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
See Also