The TXText
╰ DocumentServer Namespace
╰ MailMerge Class
The MailMerge class is a .NET component that can be used to effortlessly merge template documents with database content in .NET projects, such as ASP.NET web applications, web services or Windows services. provides an extensible framework to inject custom logic to the merge process. The TXText
╰ DocumentServer Namespace
╰ MailMerge Class
╰ FieldMerged Event
Occurs when a field has been merged. can be used to manipulate the results, but also gives access to the surrounding instance of the TXText
╰ TXTextControl Namespace
╰ TableCell Class
An instance of the TableCell class represents a single cell of a table in a Text Control document. .
This combination allows creative implementations such as conditional table cell colors based on specific filter instructions.
In the following sample, the table cell Qty should be highlighted red, if quantity is higher than 10, otherwise it should be green.
These conditions can be adjusted in the following dialog:
When merging the document, the conditions are evaluated in the custom events:
The class CellFilterInstructions contains the instructions for the custom filter:
public class CellFilterInstructions | |
{ | |
public double? CompareValue { get; set; } = null; | |
public RelationalOperator? Operator { get; set; } = null; | |
public Color TrueColor { get; set; } = Color.White; | |
public Color FalseColor { get; set; } = Color.White; | |
public enum RelationalOperator | |
{ | |
Equals = 0, | |
NotEqual, | |
LessThan, | |
GreaterThan, | |
} | |
} |
This class is serialized as JSON and stored in the TXText
╰ TXTextControl Namespace
╰ TableCell Class
╰ Name Property
Gets or sets the cell's name. . When a new dialog is opened, the JSON is deserialized and passed to the dialog. After changes are made, the object is stored as a serialized JSON string back in the TableCell.Name property.
// create a new dialog form | |
frmCellFormat frmCellFormat = new frmCellFormat(); | |
// check, if there are existing filter instructions | |
CellFilterInstructions cellFilterInstructions = | |
(CellFilterInstructions)JsonConvert.DeserializeObject( | |
textControl1.Tables.GetItem().Cells.GetItem().Name, | |
typeof(CellFilterInstructions)); | |
if (cellFilterInstructions == null) | |
{ | |
// create new instructions | |
Color cCellBackColor = textControl1.Tables.GetItem().Cells.GetItem().CellFormat.BackColor; | |
cellFilterInstructions = new CellFilterInstructions() | |
{ | |
TrueColor = cCellBackColor, | |
FalseColor = cCellBackColor | |
}; | |
} | |
// show the form | |
frmCellFormat.CellFilterInstructions = cellFilterInstructions; | |
if (frmCellFormat.ShowDialog() == DialogResult.OK) | |
{ | |
// save the instructions in the Name property | |
textControl1.Tables.GetItem().Cells.GetItem().Name = | |
JsonConvert.SerializeObject(frmCellFormat.CellFilterInstructions); | |
} |
In the FieldMerging event, the instructions are evaluated and the custom background color is applied to the cell that is returned in the event arguments:
private void MailMerge_FieldMerged(object sender, | |
TXTextControl.DocumentServer.MailMerge.FieldMergedEventArgs e) | |
{ | |
// custom field handling | |
if (e.TableCell == null) | |
return; | |
// if TableCell.Name has instructions, create a CellFilterInstructions object | |
// and evaluate the instructions and set the table cell color | |
if (e.TableCell.Name != "") | |
{ | |
CellFilterInstructions instructions = | |
(CellFilterInstructions)JsonConvert.DeserializeObject( | |
e.TableCell.Name, | |
typeof(CellFilterInstructions)); | |
// retrieve the color | |
Color? color = instructions.GetColor(e.MailMergeFieldAdapter.ApplicationField.Text); | |
// apply the color | |
if(color != null) | |
e.TableCell.CellFormat.BackColor = (Color)color; | |
} | |
} |
Test this sample on your own and download the GitHub repository.