How to: Group Data

  • 4 minutes to read

The following example demonstrates how to group Snap document data using the Snap Report API.

To start formatting document content, prepare the document for modification by doing the following.

  1. Load the document template using the RichEditDocumentServer.LoadDocumentTemplate method.
  2. Call the ISnapFieldOwner.CreateSnList method to create a new Snap list or retrieve one of the existed lists by calling the ISnapFieldOwner.FindListByName method.
  3. Convert the document fields to Snap Fields. To do that, call the ISnapFieldOwner.ParseField method. Note that without parsing, you won’t be able to modify the document.
  4. Start modifying the list by calling the SnapEntity.BeginUpdate method.

The next step is to start grouping data. To do that, perform the following steps.

  1. Create a new SnapListGroupParam object. The name of the field to be used as a grouping criteria and a sort order type must be passed to the object’s constructor method.
  2. Create a new SnapListGroupInfo object by calling the SnapListGroups.CreateSnapListGroupInfo method with the passed SnapListGroupParam object.
  3. Add the created SnapListGroupInfo object to the corresponding list collection. To do that, access the collection through the SnapList.Groups property and call the Add method.

You can also create a header and footer for the group. To complete this task, follow the steps below.

To finalize the modification, call the SnapEntity.EndUpdate method. Call the FieldCollection.Update method to update the list fields. Note that without updating, the created modifications will not take effect.

server.LoadDocumentTemplate("Template.snx")
Dim list As SnapList = server.Document.FindListByName("Data Source 11")
server.Document.ParseField(list.Field)
list.BeginUpdate()
list.EditorRowLimit = 100500

' Add a header to the Snap list.                                                                   
Dim listHeader As SnapDocument = list.ListHeader
Dim listHeaderTable As Table = listHeader.Tables(0)


Dim group As SnapListGroupInfo = list.Groups.CreateSnapListGroupInfo(New SnapListGroupParam("CategoryID", ColumnSortOrder.Ascending))
list.Groups.Add(group)

' Add a group header.
Dim groupHeader As SnapDocument = group.CreateHeader()
Dim headerTable As Table = groupHeader.Tables.Create(groupHeader.Range.End, 1, 1)
headerTable.SetPreferredWidth(50 * 100, WidthType.FiftiethsOfPercent)
Dim groupHeaderCells As TableCellCollection = 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.
Dim groupFooter As SnapDocument = group.CreateFooter()
Dim footerTable As Table = groupFooter.Tables.Create(groupFooter.Range.End, 1, 1)
footerTable.SetPreferredWidth(50 * 100, WidthType.FiftiethsOfPercent)
Dim groupFooterCells As TableCellCollection = 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

list.EndUpdate()
'list.Field.Update();