Skip to main content
All docs
V17.2

DevExpress v25.1 Update — Your Feedback Matters

Our What's New in v25.1 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

Lesson 6 - Bind a Scheduler to MS Access Database at Design Time

  • 8 minutes to read

The following example demonstrates how to bind a SchedulerControl to an MDB database at design time in Microsoft Visual Studio.

To bind the SchedulerControl to a BindingSource, follow the instructions below.

#Create a Database

  1. Create a new Microsoft Access database. Name it SchedulerTest.accdb.
  2. Create Appointments and Resources tables as described in the Scheduler Data Source - Microsoft Access Database topic.
  3. Add three records to the Resource table using the following SQL query syntax. Each Insert statement should be run as a separate query.

    
    INSERT INTO Resources ( ResourceID, ResourceName, Color, Picture ) VALUES ('1', 'Resource One', NULL, NULL);
    INSERT INTO Resources ( ResourceID, ResourceName, Color, Picture ) VALUES ('2', 'Resource Two', NULL, NULL);
    INSERT INTO Resources ( ResourceID, ResourceName, Color, Picture ) VALUES ('3', 'Resource Three', NULL, NULL);
    
  4. Insert a bitmap image into the Picture field for each record in the Resources table. To accomplish this, open the Resources table in the Datasheet View, right-click the Picture field ans select Insert Object… A dialog window appears. Select Create New, Bitmap Image as the Object Type. After clicking OK the Paint Resource Editor is invoked. Click Paste, Paste From and select an image file to load.

    BindToDataVS2015_InsertBitmapToDatabase

#Create Data Objects

  1. Create a new Windows Forms Application project in Visual Studio.
  2. Drop the BindingSource component from the Data Toolbox tab to the form. A new component (bindingSource1) will appear in the tray.
  3. Select the bindingSource1 component in the tray and open the Properties window (for instance, by pressing the F4 key or by selecting VIEW | Properties Window in the main menu). Then click the drop-down arrow for the DataSource property and select Add Project Data Source….

    BindToDataVS2015_01

    The Data Source Configuration Wizard will be invoked.

  4. Select the Database icon and click Next.

    BindToDataVS2012_02.png

  5. On the next page, select the Dataset database model and click Next.

    BindToDataVS2012_DatbaseModel.png

  6. On the next page, click New Connection button and specify the path to the database in the Add Connection dialog window.

    BindToDataVS2015_025

    BindToDataVS2015_03

  7. Click Next on the following page, which asks you whether the connection string created should be saved to the configuration file.

    BindToDataVS2015_04.png

  8. The next page allows you to choose which tables need to be obtained from the database. Select the Appointments and Resources tables and click Finish.

    BindToDataVS2015_05.png

  9. Visual Studio automatically adds an instance of the schedulerTestDataSet class to the tray when connecting to the database in the manner shown above. You can check whether or not the generated dataset contains the “Appointments” and “Resources” tables. To do this, double-click SchedulerTestDataSet1.xsd in Solution Explorer or select the schedulerTestDataSet component in the tray and click the Edit in DataSet Designer… task.

    BindToDataVS2012_06.png

    The data schema will be opened, as shown below.

    BindToDataVS2012_07.png

#Bind the Scheduler to Data

  1. Drop the SchedulerControl item from the DX.17.2: Scheduling Toolbox tab to the form.
  2. Click the smart tag icon at the top right of the control (SmartTags1.png) to display its actions list. Select Dock in Parent Container. This will stretch the SchedulerControl to fill the entire form.
  3. The new SchedulerStorage component (schedulerStorage1) is automatically added to the form and assigned to the Scheduler Control (using the SchedulerControl.Storage property). The SchedulerStorage component implements all data operations for the SchedulerControl class. Hence, to bind a scheduler to data, it is necessary to provide data for the storage to which it is assigned.

    Appointment data is stored in the Appointments data table. This data should be provided to the AppointmentStorage, which is accessed via the SchedulerStorage.Appointments property. To bind the storage, set its DataSource property to the Appointments data table in bindingSource1.

    BindToDataVS2012_08.png

  4. Specify standard mappings for appointment properties. Click the smart tag of the SchedulerControl to invoke the SchedulerControl Tasks menu, and click the Mappings Wizard… link in the Appointments section to display the Mappings Wizards.

    MappingWizard.png

    In the Setup Appointment Storage window, click the Generate button. All required mappings are generated automatically. Check to see if they are correct, and click Finish.

    BindToDataVS2012_09.png

    Note

    Make sure that fields are not mapped more than once. Use the Check Mappings link in the Appointments section of the SchedulerControl Tasks menu to prevent this. Incorrect or missed mappings may result in errors that are difficult to trace when they occur.

    For more information on the mappings concept, see the Mappings topic.

  5. Click the smart tag of the SchedulerControl to invoke the SchedulerControl Tasks menu. Click the Resource Data Source drop-down arrow and select the Cars data table.

    BindToDataVS2012_CarsDatabaseSelection.png

  6. Now specify standard mappings and custom mappings for resource properties. Click the Mappings Wizard… link in the Resources section. In the Setup Resource Storage window, click the Generate button. All required mappings are generated automatically. Set the ParentId mapping to none, since it is only required for the Gantt View. Click Finish.

    BindToDataVS2012_10.png

#Provide Data at Runtime

To retrieve data from the database, call the Fill method of the corresponding TableAdapter. In this example, the SchedulerTestDataSet.Appointments and SchedulerTestDataSet.Resources tables are populated with data using the Fill methods of the AppointmentsTableAdapter and ResourcesTableAdapter objects. Microsoft Visual Studio automatically adds this code to the form’s Load event. In most cases, you do not need to add it manually or modify it.

private void Form1_Load(object sender, EventArgs e) {
    // TODO: This line of code loads data into the 'schedulerTestDataSet.Resources' table. You can move, or remove it, as needed.
    this.resourcesTableAdapter.Fill(this.schedulerTestDataSet.Resources);
    // TODO: This line of code loads data into the 'schedulerTestDataSet.Appointments' table. You can move, or remove it, as needed.
    this.appointmentsTableAdapter.Fill(this.schedulerTestDataSet.Appointments);
}

#Set Initial Scheduler Options

  • Change the Start Date

    By default, the start date of the scheduler is set to the date set in the system when the scheduler was created. Change it to the current date.

    
    this.schedulerControl1.Start = DateTime.Today;
    
  • Group Appointments by Resource

    To group the scheduler data by resource, set the SchedulerControl.GroupType property value to SchedulerGroupType.Resource.

    
    schedulerControl1.GroupType = DevExpress.XtraScheduler.SchedulerGroupType.Resource;
    
  • Set the Number of Displayed Resources

    If there are too many resources in the ResourceStorage, the scheduler area looks cluttered. To avoid this, use the SchedulerViewBase.ResourcesPerPage property to specify the number of resources shown on the screen at one time.

    
    schedulerControl1.ActiveView.ResourcesPerPage = 2;
    
  • Enable Shared Resources

    To enable an appointment to have several associated resources, set the AppointmentStorageBase.ResourceSharing property to true.

    
    schedulerStorage1.Appointments.ResourceSharing = true;
    
  • Customize the Appearance of Resource Headers

    To control how resource headers are displayed within the SchedulerControl, use the properties of the SchedulerResourceHeaderOptions object, accessed via the SchedulerOptionsView.ResourceHeaders property. To specify the size and size mode of images that are shown within resource headers in the following way.

    
    schedulerControl1.OptionsView.ResourceHeaders.ImageAlign = DevExpress.XtraScheduler.HeaderImageAlign.Top;
    schedulerControl1.OptionsView.ResourceHeaders.ImageSizeMode = DevExpress.XtraScheduler.HeaderImageSizeMode.ZoomImage;
    schedulerControl1.OptionsView.ResourceHeaders.ImageSize = new Size(140, 70);
    schedulerControl1.OptionsView.ResourceHeaders.Height = 100;
    

#Post Data Back to the Database

  1. Run the project and start editing data. The changes will be saved in the underlying DataTable objects, but they will not be saved in the database. To complete the example, write the code that posts the changes back to the database. Handle the SchedulerStorageBase.AppointmentsChanged, SchedulerStorageBase.AppointmentsDeleted and SchedulerStorageBase.AppointmentsInserted events, and include the code that saves changes to the event hander, as illustrated in the following code snippet.

        // ...
        schedulerStorage1.AppointmentsChanged += OnAppointmentChangedInsertedDeleted;
        schedulerStorage1.AppointmentsInserted += OnAppointmentChangedInsertedDeleted;
        schedulerStorage1.AppointmentsDeleted += OnAppointmentChangedInsertedDeleted;
        // ...
    private void OnAppointmentChangedInsertedDeleted(object sender, PersistentObjectsEventArgs e) {
        appointmentsTableAdapter.Update(schedulerTestDataSet);
        schedulerTestDataSet.AcceptChanges();
    }
    
  2. To avoid a concurrency exception when saving data to a database, you should update the Identifier column value of the newly created data table record. To accomplish this, handle the RowUpdated event of the table adapter as illustrated below.

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

#Observe the Result

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

BindToDataMsAcces_RunningApplication

Tip

A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=T473371.