RadScheduler for WinForms is highly customizable component that offers functionality similar to that of the Calendar included with MS Outlook. When working with RadScheduler, developers can easily bind data sources directly to it using an instance of SchedulerBindingDataSource. The provided data source can be in the form of a database or business objects. The only requirement is that the data source must conform to a specific structure and provide specific fields. Today, I will explain how to set up basic data binding in RadScheduler using SchedulerBindingDataSource and an Access database.

The Base Application

For this entry, it should be noted that the base application I will be starting with consists of a RadScheduler, RadSchedulerNavigator, and RadButton.

Base RadScheduler Application
Figure 1. The Base Application

The Data Source Model

When performing data binding with RadScheduler, the provided data source must conform to a specific layout. This layout is comprised of the following tables.

Appointments Table
Figure 2. Appointments Table
This is the table in which appointments will be stored. Note that only ID, Summary, Start, and End are required fields.

Resources Table
Figure 3. Resources Table
This is the table in which resources will be stored.

Appointments Resources Table
Figure 4. AppointmentsResources Table
This is the table that allows for a many-to-many relationship between appointments and resources.

Please note that it is possible to customize these tables and display additional fields in RadScheduler. You can read more about this feature here

Included with the installation of RadControls for WinForms are a few sample databases to help get you started with this model. These are located in the '\Examples\DataSources' folder. Their file names are SchedulerData.mdb for MS Access, and SchedulerData.mdf + SchedulerData_log.ldf for SQL Server. I will be using a modified version of SchedulerData.mdb for this example.

Binding to RadScheduler

SchedulerBindingDataSource is the component we will be using to bind data to the RadScheduler. To use it in your application, simply drag an instance of it from the ToolBox into the form designer. Once available, adding a data source is quite simple:

  • Click the Smart Tag of the SchedulerBindingDataSource instance
    • Expand 'Choose Data Source'
    • Select 'Add Project Data Source...'
      • This will open the Data Source Configuration Wizard which will allow you to select your data source.
    • Select Database and add a connection to the SchedulerData.mdb Access database.
    • Appointments, Resources, and AppointmentsResources are the required tables.

 
Data Source Setup
Figure 5. Data Source Configuration Wizard

Once the previous steps are completed, a new DataSet and BindingSource will be generated for use with the SchedulerBindingDataSource. The SchedulerBindingDataSource must then be configured to bind to the new BindingSource via its Smart Tag.

Scheduler DataSource Setup
Figure 6. SchedulerBindingDataSource Setup

Once the instance of SchedulerBindingDataSource has been correctly bound, the appointment mapping and resource mappings can be setup through its Smart Tag as well.

Edit Appointment Mapping
Figure 7. Edit Appointment Mapping

Resource Mapping
Figure 8. Edit Resource Mapping

Now that the SchedulerBindingDataSource has been configured, you can bind it to an instance of RadScheduler by simply setting the RadScheduler's DataSource to the SchedulerBindingDataSource via its Smart Tag.

Populating the Data Source

Now that the the RadScheduler has been configured to receive data from our correctly configured SchedulerBindingDataSource, we need to make sure to populate our data source with data.

The following code can be used to populate the DataSet.

AppointmentsTableAdapter appointmentsTableAdapter = new AppointmentsTableAdapter();
ResourcesTableAdapter resourcesTableAdapter = new ResourcesTableAdapter();
AppointmentsResourcesTableAdapter appointmentsResourcesTableAdapter = new AppointmentsResourcesTableAdapter();
              
appointmentsTableAdapter.Fill(schedulerDataSet.Appointments);
resourcesTableAdapter.Fill(schedulerDataSet.Resources);
appointmentsResourcesTableAdapter.Fill(schedulerDataSet.AppointmentsResources);

Saving Appointments to the Database

Now that we can see appointments from the database in RadScheduler, we also need to make sure that the user can easily commit new appointments back to the database. This can be done through the following.

private void btnCommit_Click(object sender, EventArgs e)
{
    AppointmentsTableAdapter appointmentsTableAdapter = new AppointmentsTableAdapter();
    AppointmentsResourcesTableAdapter appointmentsResourcesTableAdapter = new AppointmentsResourcesTableAdapter();
  
    SchedulerDataSet.AppointmentsResourcesDataTable deletedChildRecords =
        this.schedulerDataSet.AppointmentsResources.GetChanges(DataRowState.Deleted)
        as SchedulerDataSet.AppointmentsResourcesDataTable;
  
    SchedulerDataSet.AppointmentsResourcesDataTable newChildRecords =
        this.schedulerDataSet.AppointmentsResources.GetChanges(DataRowState.Added)
        as SchedulerDataSet.AppointmentsResourcesDataTable;
  
    SchedulerDataSet.AppointmentsResourcesDataTable modifiedChildRecords =
        this.schedulerDataSet.AppointmentsResources.GetChanges(DataRowState.Modified)
        as SchedulerDataSet.AppointmentsResourcesDataTable;
  
    try
    {
        if (deletedChildRecords != null)
        {
            appointmentsResourcesTableAdapter.Update(deletedChildRecords);
        }
        appointmentsTableAdapter.Update(this.schedulerDataSet.Appointments);
  
        if (newChildRecords != null)
        {
            appointmentsResourcesTableAdapter.Update(newChildRecords);
        }
  
        if (modifiedChildRecords != null)
        {
            appointmentsResourcesTableAdapter.Update(modifiedChildRecords);
        }
  
        this.schedulerDataSet.AcceptChanges();
    }
    catch (Exception ex)
    {
        MessageBox.Show(string.Format("An error occurred during the update process:\n{0}", ex.Message));
    }
    finally
    {
        if (deletedChildRecords != null)
            deletedChildRecords.Dispose();
  
        if (newChildRecords != null)
            newChildRecords.Dispose();
  
        if (modifiedChildRecords != null)
            modifiedChildRecords.Dispose();
    }
}

 

Click here to download the code used in this entry...


Related Posts

Comments

Comments are disabled in preview mode.