How to: Convert a SnapList's Row Template to Paragraphs (Runtime Sample)
- 3 minutes to read
The code sample below shows how to convert a SnapList’s row template to paragraphs. When you drag and drop fields from the Data Explorer, they are inserted into a report in the following format: Field’s Display Name = Field Value.
Create a collection of data fields and fill it in the SnapDocument.BeforeInsertSnList event handler.
In the SnapDocument.PrepareSnList event handler, do the following:
Call the ISnapFieldOwner.ParseField method for each data field in the template to convert them to Snap Fields.
If the returned object is not null, call the SnapEntity.BeginUpdate method to initiate the list’s update session.
Clear the list’s header and row templates.
For each field, call the AppendText method to insert the field’s display name (the DataFieldInfo.DisplayName property value). Use the ISnapFieldOwner.CreateSnText method to insert the field value. Call the ParagraphCollection.Append method to start a new paragraph.
Append a new paragraph and call the SnapEntity.EndUpdate to finalize the list update.
Reset the dataFields collection.
using System;
using System.Collections.Generic;
using System.Data;
using DevExpress.Snap.Core.API;
//...
List<DataFieldInfo> dataFields;
void Document_BeforeInsertSnList(object sender, BeforeInsertSnListEventArgs e)
{
//Fill the data fields collection
this.dataFields = e.DataFields;
}
void Document_PrepareSnList(object sender, PrepareSnListEventArgs e)
{
for (int i = 0; i < e.Template.Fields.Count; i++)
{
//Convert each field to snap field
SnapList list = e.Template.ParseField(field) as SnapList;
//Check whether the returned object is not null
if(list !=null)
{
//Open the snap entity for modification:
list.BeginUpdate();
//Clear the list header and row templates:
list.ListHeader.Delete(list.ListHeader.Range);
SnapDocument template = list.RowTemplate;
template.Delete(template.Range);
//Insert each data field as simple text:
foreach(DataFieldInfo dataField in this.dataFields)
{
template.AppendText(string.Format("{0} = ", dataField.DisplayName));
template.CreateSnText(template.Range.End, dataField.DataPaths[dataField.DataPaths.Length - 1]);
template.AppendParagraph();
}
//Insert a new paragraph:
template.AppendParagraph();
//End the list's update:
list.EndUpdate();
}
}
this.dataFields = null;
}