ASPxGridView.GetGroupedColumns() Method
Returns a collection of columns involved in grouping.
Namespace: DevExpress.Web
Assembly: DevExpress.Web.v19.1.dll
Declaration
public ReadOnlyCollection<GridViewDataColumn> GetGroupedColumns()
Public Function GetGroupedColumns As ReadOnlyCollection(Of GridViewDataColumn)
Returns
Data displayed within the ASPxGridView can be grouped by multiple columns. The GetGroupedColumns property provides access to the columns involved in grouping. This property can be used if you need to traverse through the grouped columns to perform operations on them.
To learn more, see Grouping Overview.
Example
This example demonstrates how to save/load the layout data of the grouped and expanded rows during callbacks. Pay attention to the following features:- The Dictionary<string, string> contains grouped grid's layouts saved by the user. This Dictionary is kept in a Session.
- It is possible to load the last saved layout from by clicking the "Load Layout" button.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="DevExpress.Web.ASPxGridView.v10.1, Version=10.1.6.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web.ASPxGridView" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.ASPxEditors.v10.1, Version=10.1.6.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web.ASPxEditors" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.v10.1, Version=10.1.6.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web.ASPxCallback" 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 runat="server">
<title>How to save/store ASPxGridView's layout</title>
</head>
<body>
<form runat="server" id="form1">
<dx:ASPxGridView ID="grid" runat="server" AutoGenerateColumns="False" ClientInstanceName="grid"
DataSourceID="SqlDataSource1" KeyFieldName="ProductID" OnCustomCallback="grid_CustomCallback">
<SettingsPager EllipsisMode="None" PageSize="15">
</SettingsPager>
<Columns>
<dx:GridViewDataTextColumn FieldName="ProductID" ReadOnly="True" VisibleIndex="0">
<EditFormSettings Visible="False" />
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="ProductName" VisibleIndex="1">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="CategoryID" VisibleIndex="2">
</dx:GridViewDataTextColumn>
</Columns>
<Settings ShowGroupPanel="True" />
</dx:ASPxGridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [ProductID], [ProductName], [CategoryID] FROM [Products]">
</asp:SqlDataSource>
<dx:ASPxButton ID="buttonLoad" runat="server" AutoPostBack="False" Text="Load Layout">
<ClientSideEvents Click="function(s, e) {
grid.PerformCallback();
}" />
</dx:ASPxButton>
<dx:ASPxButton ID="buttonSave" runat="server" AutoPostBack="False" Text="Save Layout">
<ClientSideEvents Click="function(s, e) {
callback.PerformCallback();
}" />
</dx:ASPxButton>
<dx:ASPxCallback ID="callback" runat="server" ClientInstanceName="callback" OnCallback="callback_Callback">
</dx:ASPxCallback>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxGridView;
using System.Collections.ObjectModel;
using System.Text;
public partial class _Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
if (Session["layout"] == null)
Session["layout"] = new Dictionary<int, string>();
}
protected void callback_Callback(object source, DevExpress.Web.ASPxCallback.CallbackEventArgs e) {
grid.DataBind();
Dictionary<int, string> dictionary = Session["layout"] as Dictionary<int, string>;
if (dictionary != null) {
if (grid.GetGroupedColumns().Count == 0) {
Session["fields"] = string.Empty;
Session["rowscount"] = 0;
return;
}
dictionary.Clear();
for (int i = 0; i < grid.VisibleRowCount; i++) {
if (grid.IsGroupRow(i) && grid.IsRowExpanded(i)) {
dictionary[i] = "Saved";
}
}
Session["rowscount"] = grid.VisibleRowCount;
ReadOnlyCollection<GridViewDataColumn> cols = grid.GetGroupedColumns() as ReadOnlyCollection<GridViewDataColumn>;
StringBuilder sb = new StringBuilder();
foreach (GridViewDataColumn item in cols) {
sb.Append(item.FieldName);
sb.Append(';');
}
sb.Remove(sb.Length - 1, 1);
Session["fields"] = sb.ToString();
}
}
protected void grid_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e) {
Dictionary<int, string> dictionary = Session["layout"] as Dictionary<int, string>;
if (dictionary == null || Session["rowscount"] == null || Session["fields"] == null)
return;
for (int i = 0; i < grid.Columns.Count; i++)
grid.UnGroup(grid.Columns[i]);
grid.CollapseAll();
string[] fields = Session["fields"].ToString().Split(';');
if (fields[0] == string.Empty)
return;
foreach (string field in fields) {
grid.GroupBy(grid.Columns[field]);
}
for (int i = 0; i < Convert.ToInt32(Session["rowscount"]); i++) {
if (dictionary.ContainsKey(i))
grid.ExpandRow(i);
}
}
}
<%@ Page Language="vb" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<%@ Register Assembly="DevExpress.Web.ASPxGridView.v10.1, Version=10.1.6.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web.ASPxGridView" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.ASPxEditors.v10.1, Version=10.1.6.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web.ASPxEditors" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.v10.1, Version=10.1.6.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
Namespace="DevExpress.Web.ASPxCallback" 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 runat="server">
<title>How to save/store ASPxGridView's layout</title>
</head>
<body>
<form runat="server" id="form1">
<dx:ASPxGridView ID="grid" runat="server" AutoGenerateColumns="False" ClientInstanceName="grid"
DataSourceID="SqlDataSource1" KeyFieldName="ProductID" OnCustomCallback="grid_CustomCallback">
<SettingsPager EllipsisMode="None" PageSize="15">
</SettingsPager>
<Columns>
<dx:GridViewDataTextColumn FieldName="ProductID" ReadOnly="True" VisibleIndex="0">
<EditFormSettings Visible="False" />
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="ProductName" VisibleIndex="1">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn FieldName="CategoryID" VisibleIndex="2">
</dx:GridViewDataTextColumn>
</Columns>
<Settings ShowGroupPanel="True" />
</dx:ASPxGridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [ProductID], [ProductName], [CategoryID] FROM [Products]">
</asp:SqlDataSource>
<dx:ASPxButton ID="buttonLoad" runat="server" AutoPostBack="False" Text="Load Layout">
<ClientSideEvents Click="function(s, e) {
grid.PerformCallback();
}" />
</dx:ASPxButton>
<dx:ASPxButton ID="buttonSave" runat="server" AutoPostBack="False" Text="Save Layout">
<ClientSideEvents Click="function(s, e) {
callback.PerformCallback();
}" />
</dx:ASPxButton>
<dx:ASPxCallback ID="callback" runat="server" ClientInstanceName="callback" OnCallback="callback_Callback">
</dx:ASPxCallback>
</form>
</body>
</html>
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports DevExpress.Web.ASPxGridView
Imports System.Collections.ObjectModel
Imports System.Text
Partial Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Session("layout") Is Nothing Then
Session("layout") = New Dictionary(Of Integer, String)()
End If
End Sub
Protected Sub callback_Callback(ByVal source As Object, ByVal e As DevExpress.Web.ASPxCallback.CallbackEventArgs)
grid.DataBind()
Dim dictionary As Dictionary(Of Integer, String) = TryCast(Session("layout"), Dictionary(Of Integer, String))
If dictionary IsNot Nothing Then
If grid.GetGroupedColumns().Count = 0 Then
Session("fields") = String.Empty
Session("rowscount") = 0
Return
End If
dictionary.Clear()
For i As Integer = 0 To grid.VisibleRowCount - 1
If grid.IsGroupRow(i) AndAlso grid.IsRowExpanded(i) Then
dictionary(i) = "Saved"
End If
Next i
Session("rowscount") = grid.VisibleRowCount
Dim cols As ReadOnlyCollection(Of GridViewDataColumn) = TryCast(grid.GetGroupedColumns(), ReadOnlyCollection(Of GridViewDataColumn))
Dim sb As New StringBuilder()
For Each item As GridViewDataColumn In cols
sb.Append(item.FieldName)
sb.Append(";"c)
Next item
sb.Remove(sb.Length - 1, 1)
Session("fields") = sb.ToString()
End If
End Sub
Protected Sub grid_CustomCallback(ByVal sender As Object, ByVal e As DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs)
Dim dictionary As Dictionary(Of Integer, String) = TryCast(Session("layout"), Dictionary(Of Integer, String))
If dictionary Is Nothing OrElse Session("rowscount") Is Nothing OrElse Session("fields") Is Nothing Then
Return
End If
For i As Integer = 0 To grid.Columns.Count - 1
grid.UnGroup(grid.Columns(i))
Next i
grid.CollapseAll()
Dim fields() As String = Session("fields").ToString().Split(";"c)
If fields(0) = String.Empty Then
Return
End If
For Each field As String In fields
grid.GroupBy(grid.Columns(field))
Next field
For i As Integer = 0 To Convert.ToInt32(Session("rowscount")) - 1
If dictionary.ContainsKey(i) Then
grid.ExpandRow(i)
End If
Next i
End Sub
End Class
The following code snippet (auto-collected from DevExpress Examples) contains a reference to the GetGroupedColumns() method.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.
See Also