Skip to main content

How to: Bind a Scheduler to MS Access Database (Part 1) (legacy)

  • 7 minutes to read

Note

You are viewing documentation for the legacy WPF Scheduler control. If you’re starting a new project, we strongly recommend that you use a new control declared in the DevExpress.Xpf.Scheduling namespace. If you decide to upgrade an existing project in order to switch to the updated scheduler control, see the Migration Guidelines document.

This example consists of the following sections.

Create a New Project and Add a Scheduler Control

  1. Create a new WPF Application project, specify its name as MyWpfApplication, and open the MainWindow.xaml file in the Designer.
  2. Add the SchedulerControl item to your project. For this, double-click the SchedulerControl item from the DX.23.2: Scheduling group. Specify CarSchedulingControl as the name of the newly added SchedulerControl.

    Scheduler_Lesson1_Im1

  3. Set the HorizontalAlignment and VerticalAlignment property values to Stretch. This will stretch the scheduler control to fill the entire window.

    Scheduler_Lesson1_Im2

    Your XAML may look like the following. (If it does not, you can overwrite your code with the code below.)

    <Window x:Class="MyWpfApplication.MainWindow" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:dxsch="http://schemas.devexpress.com/winfx/2008/xaml/scheduler" 
        Title="MainWindow" Height="350" Width="525" > 
        <Grid> 
            <dxsch:SchedulerControl Name="CarSchedulingControl" /> 
        </Grid> 
    </Window>
    

    Note

    You can add a SchedulerControl by overwriting your MainWindow.xaml file with this code without dragging the SchedulerControl control to the window. However, in this case, you will need to manually add references to the following libraries: DevExpress.Data.v23.2, DevExpress.Xpf.Core.v23.2, DevExpress.Xpf.Scheduler.v23.2, and DevExpress.XtraScheduler.v23.2.Core.

    Normally, when adding references to these assemblies, you should select them from the Global Assembly Cache (GAC). However, if you prefer to copy them locally, or need to include them in your product installation later, you can find copies in the C:\Program Files\DevExpress 23.2\Components\Bin\Framework\ directory.

Since the most common way of using a scheduler is to bind it to a datasource, proceed to the next step to learn how.

Create Data Objects

In the next step, you will bind the scheduler to the CarScheduling table of the CarsDB database. The scheduler will represent this table as appointments.

  1. Copy the CarsDB.mdb file to the directory of your project. By default, this file is placed in the C:\Users\Public\Documents\DXperience 23.2 Demos\Data directory.
  2. Include the CarsDB.mdb file in the project by right-clicking the file, and selecting Include In Project in the invoked context menu. This will invoke the Data Source Configuration Wizard.

    Scheduler_Lesson1_Im3

    Note

    If the CarsDB.mdb is not displayed in the Solution Explorer, select Show All Files from the Project menu or click the Show All Files icon (Scheduler_Lesson1_ShowAllFilesButton) in the Solution Explorer toolbar.

  3. In the invoked window, select the Dataset database model and click Next.

    Scheduler_Lesson1_Im4

  4. The next page allows you to choose tables to be obtained from the database. Select the CarScheduling table and click Finish.

    Scheduler_Lesson1_Im5

  5. As a result, Visual Studio will generate a set of classes that support ADO.NET architecture. The main classes include:

    • CarSchedulingDataTable - represents a System.Data.DataTable object that will contain records from the “CarScheduling” table in the Cars database;
    • CarsDBDataSet - represents a System.Data.DataSet object (a collection of tables that can be related to each other). The generated DataSet contains the CarSchedulingDataTable table;
    • CarSchedulingTableAdapter - represents a TableAdapter object that can communicate with the “CarScheduling” table in the Cars database.
  6. Visual Studio automatically adds a dataset schema when connecting to the database in the manner illustrated above (the CarsDBDataSet.xsd node in Solution Explorer). You can check to see whether the dataset generated contains the CarSchedulingDataTable table. To do this, double-click CarsDBDataSet.xsd to open the designer.

    Scheduler_Lesson1_Im6

Next, bind the SchedulerControl to data and establish mappings between the fields in data tables and appointment properties.

Bind the Scheduler to Data

In this example, all appointment data is stored in the CarScheduling data table. This data should be provided to AppointmentStorage, which is accessed via the SchedulerStorage.AppointmentStorage property. To bind the storage, set the SchedulerControl.Storage.AppointmentStorage.DataSource property to the CarScheduling data table.

You will also need to populate the underlying CarSchedulingDataTable table with data from the database. To do this, use the Fill method of the CarSchedulingTableAdapter object.

using System.Windows;

namespace MyWpfApplication {
    public partial class MainWindow : Window {

        CarsDBDataSet dataSet;
        CarsDBDataSetTableAdapters.CarSchedulingTableAdapter adapter;

        public MainWindow() {
            InitializeComponent();

            this.dataSet = new CarsDBDataSet();

            // Bind the scheduler storage to appointment data. 
            this.CarSchedulingControl.Storage.AppointmentStorage.DataSource = dataSet.CarScheduling;

            // Load data into the 'CarsDBDataSet.CarScheduling' table.  
            this.adapter = new CarsDBDataSetTableAdapters.CarSchedulingTableAdapter();
            this.adapter.Fill(dataSet.CarScheduling);
        }
    }
}

Specify Mappings

Next, specify standard mappings for appointment properties by setting the AppointmentStorage.Mappings property values as shown below.

Scheduler_Lesson1_Im7

Your XAML may look like the following. (If it does not, you can overwrite your code with the code below.)

<dxsch:SchedulerStorage.AppointmentStorage>
    <dxsch:AppointmentStorage 
        DataSource = "CarScheduling">
        <dxsch:AppointmentStorage.Mappings>
            <dxsch:AppointmentMapping 
            AllDay="AllDay" 
            Description="Description" 
            End="EndTime" 
            Label="Label" 
            Location="Location" 
            ReminderInfo="ReminderInfo" 
            Start="StartTime" 
            Status="Status" 
            Subject="Subject" 
            Type="EventType" 
            RecurrenceInfo="RecurrenceInfo"/>
        </dxsch:AppointmentStorage.Mappings>
    </dxsch:AppointmentStorage>
</dxsch:SchedulerStorage.AppointmentStorage>

Customize the Scheduler

Change the Start Date - The SchedulerControl object allows you to set the start date for which the scheduler will initially show data after the project is run. To do this, set the Start property of SchedulerControl to 7/15/2010.

Scheduler_Lesson1_Im8

Changing the Active View - Initially, the SchedulerControl displays its data using the Day View. But in this instance, you will need to manipulate many appointments over several months. Here, the most appropriate view is either the Week View or the Month View. Set the ActiveViewType property value of SchedulerControl to Week.

Scheduler_Lesson1_Im9

Your XAML may look like the following. (If it does not, you can overwrite your code with the code below.)

<dxsch:SchedulerControl 
    x:Name="CarSchedulingControl" 
    ActiveViewType="Week" 
    Start="2010-07-15">

Post Data Back to the Database

At this stage, you can run the project and start editing data. The changes will be saved to the underlying DataTable object, but they will not be saved in the database. To complete this example, write the code that posts the changes in the CarSchedulingDataTable table back to the database. The SchedulerStorage.AppointmentsChanged, SchedulerStorage.AppointmentsDeleted and SchedulerStorage.AppointmentsInserted events should be handled for this purpose.

To avoid a concurrency exception when saving data to a database, update the Identifier column value of the newly created data table record. To accomplish this, handle the RowUpdated event of the table adapter.

To complete the example, use the following code:

using System.Windows;
using System.Data;
using System.Data.OleDb;
using DevExpress.XtraScheduler;

namespace MyWpfApplication {
    public partial class MainWindow : Window {

        CarsDBDataSet dataSet;
        CarsDBDataSetTableAdapters.CarSchedulingTableAdapter adapter;

        public MainWindow() {
            InitializeComponent();

            this.dataSet = new CarsDBDataSet();

            // Bind the scheduler storage to appointment data. 
            this.CarSchedulingControl.Storage.AppointmentStorage.DataSource = dataSet.CarScheduling;

            // Load data into the 'CarsDBDataSet.CarScheduling' table.  
            this.adapter = new CarsDBDataSetTableAdapters.CarSchedulingTableAdapter();
            this.adapter.Fill(dataSet.CarScheduling);

            this.CarSchedulingControl.Storage.AppointmentsInserted +=
                new PersistentObjectsEventHandler(Storage_AppointmentsModified);
            this.CarSchedulingControl.Storage.AppointmentsChanged +=
                new PersistentObjectsEventHandler(Storage_AppointmentsModified);
            this.CarSchedulingControl.Storage.AppointmentsDeleted +=
                new PersistentObjectsEventHandler(Storage_AppointmentsModified);

            this.adapter.Adapter.RowUpdated +=
                new System.Data.OleDb.OleDbRowUpdatedEventHandler(adapter_RowUpdated);
        }

        void Storage_AppointmentsModified(object sender, PersistentObjectsEventArgs e) {
            this.adapter.Adapter.Update(this.dataSet);
            this.dataSet.AcceptChanges();

        }

        private void adapter_RowUpdated(object sender, System.Data.OleDb.OleDbRowUpdatedEventArgs e) {
            if (e.Status == UpdateStatus.Continue && e.StatementType == StatementType.Insert) {
                int id = 0;
                using (OleDbCommand cmd = new OleDbCommand("SELECT @@IDENTITY", adapter.Connection)) {
                    id = (int)cmd.ExecuteScalar();
                }
                e.Row["ID"] = id;
            }
        }
    }
}

Result

Run the project. The following image shows the application at runtime.

Scheduler_Lesson1_Im10