Calculated Fields

Just like lookup fields, calculated fields are virtual fields which you can add to a table object (in this case the Tasks table). A calculated field differs from a lookup field in that rather than look up its value from another table based on a key, it will calculate its value using code.

To demonstrate this we are going to improve how the data from the Done column is presented. We have a couple of possible options:

  • Show a checkbox which is clear when the task is open, and checked when the task is done
  • Show the text Open when the task is open, and Done when the task is done.

For ease of demonstrating calculated fields, we will use the later and leave the former as an exercise for the reader.

Setting up the calculated field

Calculated fields in Data Abstract work exactly the same way as they do in any other dataset. We are going to add a virtual property IsDone to TasksPartial.cs that will return a String based on the value of the Done property. Remember to include the IgnoreProperty line to indicate that the property is not to be retrieved from the server.

[RemObjects.DataAbstract.Linq.IgnoreProperty]
public String IsDone
{
    get
    {
        if (this.Done == 0) {
            return "Open";
        } else {
            return "Done";
        }
    }
}

Now change the DataPropertyName in CustomizeDataGridView in MainForm.cs from Done to IsDone

private void CustomizeDataGridView()
{
    // snipped code

    var columnDone = new DataGridViewTextBoxColumn { DataPropertyName = "IsDone", HeaderText = "Done", Width = 50 };

    // snipped code
}

Now when you run the app you will see that the Done column now displays our text rather than 0 or 1.

App showing the calculated value for the Done column

Now that we are happy with how the data is presented to the user, its time that we add the ability to add, edit and delete tasks. Over the next few pages thats exactly what we shall do.