Lookup Fields
If you look closely at the data shown in the Priority column you will note that it shows the key of the priority record (1,2,3) that indicates the priority of the tasks rather than a more useful human readable value like Low, Medium or High. We will change that now.
Just as with local data we can set this up in Data Abstract using a lookup field. When you created the project, in addition to selecting the Tasks table from the schema, you also selected the Priorities table. This means that the DataModule
is able to access the data we need.
Unfortuantly DA LINQ does not provide support for using Lookup Fields, so instead we will need achieve the same result with code.
Setting up a Lookup Field
What we need to do is make some changes to the Tasks class, which is virtual class auto generated by the Template wizard from the Schema's and stored in TableDefinitions_Tasks.cs
. While it is possible to make the changes we need in TableDefinitions_Tasks.cs
it is not recommended, one reason is that changes would be lost any time TableDefinitions_Tasks.cs
is regenerated. A better solution is to take advantage of .NET's partial class support and extend the Tasks class.
Add a new class to the project called TasksPartial.cs
. We are going to add a static method ParsePrioritiesTable
which will create a lookup dictionary to match the priority id to the correct name. Then we will add a public property accessor which takes the priority ID and retrieves the correct value from the dictionary.
using System;
using System.Collections.Generic;
namespace DATutorial.Tasks
{
public partial class Tasks
{
private static IDictionary<Int64, String> _priorityLookup;
public static void ParsePrioritiesTable(IEnumerable<Priorities> source)
{
var priorityLookup = new Dictionary<Int64, String>();
foreach (var item in source)
{
priorityLookup.Add(item.Id, item.Name);
}
Tasks._priorityLookup = priorityLookup;
}
[RemObjects.DataAbstract.Linq.IgnoreProperty]
public String PriorityName
{
get
{
String priority;
if ((Tasks._priorityLookup != null) && Tasks._priorityLookup.TryGetValue(this.Priority, out priority))
{
return priority;
}
return this.Priority.ToString();
}
}
}
}
You will notice that before we define the new property PriorityName
the line of code [RemObjects.DataAbstract.Linq.IgnoreProperty]
. This tells the DA LINQ engine not to try and retrieve the data from the Relativity Server.
Now we need to return to the MainForm
class and make use of the new property. In loadButton_Click
after the user has logged in we shall add a call our new ParsePrioritiesTable
from the partial Tasks
class, which will parse the data retrieved from the Priorities table.
private void loadButton_Click(object sender, EventArgs e)
{
// snipped code
this.CustomizeDataGridView();
Tasks.Tasks. ParsePrioritiesTable(this._dataModule.DataAdapter.GetTable<DATutorial.Tasks.Priorities>());
// Retreive and display the ToDo List
this.tasksDataGridView.DataSource = (from x in this._dataModule.DataAdapter.GetTable<Tasks.Tasks>() select x).ToList();
}
The last change we to make is in the CustomizeDataGridView
method. Change the DataPropertyName
for the columnPriority
variable from Priority
to the name of our new virtual property PriorityName
.
private void CustomizeDataGridView()
{
// snipped code
var columnPriority = new DataGridViewTextBoxColumn { DataPropertyName = "PriorityName", HeaderText = "Priority", Width = 75 };
}
Now when you run the app, instead of seeing 1 in the priority column, you see Low.
On the next page we will change the Done column to show Done and Open instead of 1 and 0.