Search
Tutorial 9: Binding to a Data Source

This tutorial will show you how to integrate database serialization in your project.

1. Create and initialize a new Windows Forms application project

Follow steps 1 through 3 from Tutorial 1: Getting Started.

Set CurrentView to WeekRange, Date to Jan 1, 2006 and EndDate to Mar 1, 2006, respectively.

2. Add the data source

Select Data -> Add New Data Source from the Visual Studio menu. This will display the Data Source Configuration Wizard, which will automate the process of adding a new data source to your application.

In the dialog that appears, select 'Database', then click 'Next'.

On the following step click the 'New Connection' button. Since we will use the Planner.mdb file for a database, select 'Microsoft Access Database File' for the type of the data source. Locate the mdb file (which is usually available in the main installation folder) and click 'OK'.

When you click 'Next' to go to the next step of the wizard will ask you whether to copy the database file to the output directory of your project each time it is build. Confirm. Save the connection string in the configuration file of your application in the next step and continue.

Then you need to select the database objects you would like to be included in your data set. Mark all tables. Finally, enter the name of the data set and click 'Finish'. 

3. Rebuild the project

In order to use the data adapters created by the wizard in the previous step you need to rebuild your project. At this time the project should compile without errors. When the project is compiled, a new component category will appear in the Visual Studio Toolbox. The category is named after your project (that is, 'Tutorial9 Components' in this particular case) and contains the automatically created data set as well as data adapter wrappers for all selected tables.

4. Bind MindFusion.Scheduling to the data source

Select the Calendar control on the form and go to the Properties window. Locate the DataSource property under the Data category and click the down arrow, which appears next to the property value. In the drop-down window expand 'Other Data Sources'. The data set we created in step 2 should appear under 'Project Data Sources'. Click on it to select it as a data source. This will automatically create an instance of the data set on the form and will bind this instance to the Calendar control through a new BindingSource object.

To associate the tables from the data set with the appropriate data collections in the Calendar control, set the values of the data member properties to the name of their corresponding table. For example, the ContactsDataMember property should be set to "Contacts", and so on. Once all data members are set, continue to the next step.

Navigate back to the 'Tutorial9 Components' section in the toolbox and create one instance of each table adapter, by dragging the adapter from the toolbox to the form.

5. Perform serialization

Add two buttons on the form named 'Save' and 'Load' accordingly. Create an event handler for each of the buttons by double-clicking on them. In the event handler of the 'Save' button add the following code.

C#  Copy Code

calendar1.SaveToDataSource(
    reminderTableAdapter1,
    styleTableAdapter1,
    customBrushesTableAdapter1,
    contactTableAdapter1,
    locationTableAdapter1,
    resourceTableAdapter1,
    taskTableAdapter1,
    itemTableAdapter1,
    itemContactsTableAdapter1,
    itemResourcesTableAdapter1,
    recurrenceTableAdapter1,
    recurrenceExceptionTableAdapter1,
    recurrenceExceptionsTableAdapter1);

Visual Basic  Copy Code

calendar1.SaveToDataSource( _
    reminderTableAdapter1, _
    styleTableAdapter1, _
    customBrushesTableAdapter1, _
    contactTableAdapter1, _
    locationTableAdapter1, _
    resourceTableAdapter1, _
    taskTableAdapter1, _
    itemTableAdapter1, _
    itemContactsTableAdapter1, _
    itemResourcesTableAdapter1, _
    recurrenceTableAdapter1, _
    recurrenceExceptionTableAdapter1, _
    recurrenceExceptionsTableAdapter1)

The code above assumes that all objects created on the form so far use their default names. If you have changed the name of an object, you have to update the event handler code to reflect this.

The contents of the 'Load' button will look like this:

C#  Copy Code

calendar1.Schedule.Clear();
plannerDataSet.Clear();

reminderTableAdapter1.Fill(plannerDataSet.Reminder);
styleTableAdapter1.Fill(plannerDataSet.Style);
customBrushesTableAdapter1.Fill(plannerDataSet.CustomBrushes);
contactTableAdapter1.Fill(plannerDataSet.Contact);
locationTableAdapter1.Fill(plannerDataSet.Location);
resourceTableAdapter1.Fill(plannerDataSet.Resource);
taskTableAdapter1.Fill(plannerDataSet.Task);
itemTableAdapter1.Fill(plannerDataSet.Item);
itemContactsTableAdapter1.Fill(plannerDataSet.ItemContacts);
itemResourcesTableAdapter1.Fill(plannerDataSet.ItemResources);
recurrenceTableAdapter1.Fill(plannerDataSet.Recurrence);
recurrenceExceptionTableAdapter1.Fill(plannerDataSet.RecurrenceException);
recurrenceExceptionsTableAdapter1.Fill(plannerDataSet.RecurrenceExceptions);

calendar1.LoadFromDataSource();

Visual Basic  Copy Code

calendar1.Schedule.Clear()
plannerDataSet.Clear()

reminderTableAdapter1.Fill(plannerDataSet.Reminder)
styleTableAdapter1.Fill(plannerDataSet.Style)
customBrushesTableAdapter1.Fill(plannerDataSet.CustomBrushes)
contactTableAdapter1.Fill(plannerDataSet.Contact)
locationTableAdapter1.Fill(plannerDataSet.Location)
resourceTableAdapter1.Fill(plannerDataSet.Resource)
taskTableAdapter1.Fill(plannerDataSet.Task)
itemTableAdapter1.Fill(plannerDataSet.Item)
itemContactsTableAdapter1.Fill(plannerDataSet.ItemContacts)
itemResourcesTableAdapter1.Fill(plannerDataSet.ItemResources)
recurrenceTableAdapter1.Fill(plannerDataSet.Recurrence)
recurrenceExceptionTableAdapter1.Fill(plannerDataSet.RecurrenceException)
recurrenceExceptionsTableAdapter1.Fill(plannerDataSet.RecurrenceExceptions)

calendar1.LoadFromDataSource()

6. Build and run

Compile and run the application. Try saving and loading schedules by clicking on the two buttons.