Adding formulas to table cells

Windows Forms User's Guide > Howto: Use Spreadsheet Formulas in Tables

Consider a value of a table cell that is calculated dynamically based on other table cell values. Typically, a SUM formula is inserted at the end of a table to create a total sum value. Or a tax value that is calculated based on a tax percentage value and the calculated total sum.

TX Text Control supports 60+ formulas including most mathematical and trigonometric formulas compatible to Microsoft Excel. After starting the sample, a document is loaded that contains a table showing a cash flow calculation.

Now, set the input position into any table cell with a number that doesn't have a green background and change it. Watch the cells with a green background changing automatically based on the formula calculation.

In order to insert a new formula into a table cell, set the input position into any cell. Then select the formula function from the function dropdown box:

Then click the Add function button right next to the dropdown.

Now, type in the range of your formula. For example: SUM(B1:B4). Confirm the formula by clicking the check button right next to the formula text box. In the code, this string is simply passed to the TableCell.Formula property:

[C#]
private void ApplyFormula()
{
 try
 {
  textControl1.Tables.GetItem().Cells.GetItem().Formula = tstbFormula.Text;
 }
 catch (Exception exc) // Let TX Text Control do the validation
 {
  MessageBox.Show(exc.Message, "Formula Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
 }
}
[Visual Basic]
Private Sub ApplyFormula()
 Try
  textControl1.Tables.GetItem().Cells.GetItem().Formula = tstbFormula.Text;
 Catch exc As Exception ' Let TX Text Control do the validation
  MessageBox.Show(exc.Message, "Formula Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
 End Try
End Sub

When changing the values of the referenced table cells, you can see the value changing automatically at your formula cell.