RadiantQ WPF Gantt
Binding to data using ADO.NET Entity Model
Previous Topic  Next Topic 

Use this option when you want the bind the control to tabular data in a database, rather than in an XML file.


We can use WPF's native data access technology - ADO.NET Entity Model to bring data to the client from the data base and vice versa.


This topic provides you minimal code to help you understand the concepts, please refer to the GanttSQLDataBinding sample to see the full source code.



Follow these steps to create such an application:


Step 1: Create the control


Create an empty WPF application and add this to the MainPage XAML:


<gantt:GanttControl x:Name="ganttControrl1" Width="800" Height="600" />


Step 2: Table Field Requirements


Your table should have at least these fields (the actual name of the field can be anything, we can setup the mapping in XAML):



To represent the EndTime, your table requires one of the following fields:


You can optionally include these fields as well:


Assuming we have an EndTime rather than an Effort field in the table, you can setup mappings to your data source as follows:


             <gantt:GanttControl x:Name="ganttControl1" Grid.Column="1"

                           IDBinding="{Binding ID}" NameBinding="{Binding TaskName}"

                     IndentLevelBinding="{Binding IndentLevel}"

                     StartTimeBinding="{Binding StartDate}"

                     EffortBinding="{Binding EndDate, Converter={StaticResource endTimeToEffortConverter}}"

                     ProgressPercentBinding="{Binding ProgressPercent}" SortOrderBinding="{Binding SortOrder}"

                     PredecessorIndicesBinding="{Binding PredecessorIndices}"

                     PreferredStartTimeBinding="{Binding PreferredStartTime}"

                     DescriptionBinding="{Binding Description}" AssignedResourcesBinding="{Binding AssignedResources}"/>


2 things of note here are the StartTimeBinding and the EffortBinding settings.


a) The EffortBinding has a converter which converts the EndDate in the data source to a TimeSpan based Effort and vice versa.


b) The StartTimeBinding has a custom converter because when the start time changes we want to change the EndDate of the bound object as well, we do this in this custom converter.


This is illustrated in the above mentioned GanttSQLDataBinding sample as well as in the <install path>/Samples/ProjectGantt/DataBinding/GanttControlBindingToEndTime. You can also find the source code for these converters there.


Step 3: Create a ADO.NET Entity Model


Please refer to this online documentation on Binding WPF Controls to an Entity Data Model to setup a ADO.NET Entity Model.


This will create data classes in your WPF application representing the bound data.


Step 4: Bind the GanttControl to the entity list


Sort the entity list by a field (if there is a SortOrder field) and then create a list that could be bound to the GanttControl as follows:


            IEnumerable<Task> tasks = this.taskEntities.Tasks;

            // Order by the SortOrder to preserve end-user reorder of the task items.

            IOrderedEnumerable<Task> orderedTasks = tasks.OrderBy(task => task.SortOrder);

            foreach (Task task in tasks)

            {

                // This is a ObservableCollection<Task>

                tasksBindingCollection.Add(task);

            }


            this.ganttControl1.ItemsSource = this.tasksBindingCollection;


Step 5: Listen to changes in the items and the list


Now any changes (start time, dependencies, parent/child relationship, etc.) made to the items will be automatically updated in the bound data classes. More on this topic is discussed here.


To listen to changes in the model, listen to PropertyChanged events in each of the data bound tasks as illustrated in the sample.


You can also easily add new items to the GanttControl and the model and also delete items from the GanttControl and service context.


Step 6: CRUD Operations


Edits made to the existing tasks in the gantt are persisted into the data model automatically.


Adding and deletion of tasks require a specific pattern as illustrated in the sample mentioned above.


Note: Please note that tasks added to the data model are not automatically reflected in the gantt, please follow the pattern mentioned above.


Step 7: Persist Changes


To persist the changes made in the data classes, simply call this:


this.taskEntities.SaveChanges();


This will persist all the changes, including any addition and deletion of rows back into the database.




� RadiantQ 2009 - 2019. All Rights Reserved.