Skip to main content
A newer version of this page is available. .

How to: Group Data (Runtime Sample)

  • 2 minutes to read

This document describes how to group Snap list data in code. To learn how to accomplish this task by using the Snap graphical user interface, see the Group Document Data topic.

The method illustrated in this code sample groups Snap list data by proceeding with the following steps.

  1. Create a SnapListGroupInfo by passing the required grouping criteria and sort order to the SnapListGroups.CreateSnapListGroupInfo method.
  2. Add the created SnapListGroupInfo to the SnapList.Groups collection.
  3. To add a group header, call the SnapListGroupInfo.CreateHeader method.
  4. To add a group footer, call the SnapListGroupInfo.CreateFooter method.
using DevExpress.Data;
using DevExpress.Snap.Core.API;
using DevExpress.XtraRichEdit.API.Native;
// ...
        private void GroupList(SnapList list) {
            // Add grouping to the Snap list.
            SnapListGroupInfo group = list.Groups.CreateSnapListGroupInfo(
                new SnapListGroupParam("CategoryID", ColumnSortOrder.Ascending));
            list.Groups.Add(group);

            // Add a group header.
            SnapDocument groupHeader = group.CreateHeader();
            Table headerTable = groupHeader.Tables.Create(groupHeader.Range.End, 1, 1);
            headerTable.SetPreferredWidth(50 * 100, WidthType.FiftiethsOfPercent);
            TableCellCollection groupHeaderCells = headerTable.FirstRow.Cells;
            groupHeader.InsertText(groupHeaderCells[0].ContentRange.End, "Category ID: ");
            groupHeader.CreateSnText(groupHeaderCells[0].ContentRange.End, "CategoryID");

            // Customize the group header formatting.
            groupHeaderCells[0].BackgroundColor = System.Drawing.Color.LightGray;
            groupHeaderCells[0].Borders.Bottom.LineColor = System.Drawing.Color.White;
            groupHeaderCells[0].Borders.Left.LineColor = System.Drawing.Color.White;
            groupHeaderCells[0].Borders.Right.LineColor = System.Drawing.Color.White;
            groupHeaderCells[0].Borders.Top.LineColor = System.Drawing.Color.White;

            // Add a group footer.
            SnapDocument groupFooter = group.CreateFooter();
            Table footerTable = groupFooter.Tables.Create(groupFooter.Range.End, 1, 1);
            footerTable.SetPreferredWidth(50 * 100, WidthType.FiftiethsOfPercent);
            TableCellCollection groupFooterCells = footerTable.FirstRow.Cells;
            groupFooter.InsertText(groupFooterCells[0].ContentRange.End, "Count = ");
            groupFooter.CreateSnText(groupFooterCells[0].ContentRange.End,
                @"CategoryID \sr Group \sf Count");

            // Customize the group footer formatting.
            groupFooterCells[0].BackgroundColor = System.Drawing.Color.LightGray;
            groupFooterCells[0].Borders.Bottom.LineColor = System.Drawing.Color.White;
            groupFooterCells[0].Borders.Left.LineColor = System.Drawing.Color.White;
            groupFooterCells[0].Borders.Right.LineColor = System.Drawing.Color.White;
            groupFooterCells[0].Borders.Top.LineColor = System.Drawing.Color.White;
        }
See Also