ComponentOne GanttView for WinForms
Step 2: Configuring the Data Source
Data Binding: GanttView for WinForms > Step 2: Configuring the Data Source

In this step, you start by configuring a data source to your project and then adding code to synchronize data with the GanttView control.

  1. To add a data source to your project, click View | Other Windows | Data Sources.
  2. In Data Sources window, click Add New Data Source link to open Data Source Configuration Wizard window. Select Database as the Data Source Type and then click Next as shown below.

  3. After clicking Next, select Dataset as the database model in Data Source Configuration Wizard window and click Next as shown below.

    Selecting Dataset

  4. Select Data Connection by browsing C1NWind.mdb database file in your system. Test the connection and click Next. Save the connection string to add the database file to your project and click Next.
  5. Choose Tables as Database Object Model in Data Source Configuration Wizard window as shown below.

    Choosing database objects

  6. For this data binding topic, select Calendars, Properties, Resources and Tasks tables as object models from the drop-down list appearing under Tables and click Finish as shown below.

  7. Navigate to the Toolbox again and locate the BindingSource icon in Data. Double-click the BindingSource icon to add it to the Design View.
  8. Click the BindingSource once and set the DataSource property to c1NWindDataSet and DataMember as Tasks using the drop-down arrow in the Properties window. Delete the BindingSource from the design view after setting these properties.
  9. Repeat Step 8 for Calendars, Resources and Properties tables to set their DataSource as c1NWindDataSet and DataMember as Calendars, Resources and Properties, respectively.
  10. The above steps creates adapters for the added datasets in your project. These adapters appear in design view similar to the following image.

    Table adapters appearing on designer

  11. Add LoadData() and SaveData() methods in the code to load and save data from the data source. These methods get invoked in Form1_Load and Form1_FormClosing events, respectively, as shown in the following code.
    Visual Basic
    Copy Code
    Imports C1.Win.C1GanttView
    
    Public Class Form1
    
        Public Sub New()
            InitializeComponent()
        End Sub
    
        Private Sub LoadData()
            Try
                Me.TasksTableAdapter.Fill(Me.C1NWindDataSet.Tasks)
    
                Me.ResourcesTableAdapter.Fill(Me.C1NWindDataSet.Resources)
    
                Me.PropertiesTableAdapter.Fill(Me.C1NWindDataSet.Properties)
    
                Me.CalendarsTableAdapter.Fill(Me.C1NWindDataSet.Calendars)
    
            Catch ex As Exception
    
                MessageBox.Show(ex.ToString)
    
            End Try
        End Sub
    
        Private Sub SaveData()
            Try
                Me.TasksTableAdapter.Update(C1NWindDataSet.Tasks)
    
                Me.ResourcesTableAdapter.Update(C1NWindDataSet.Resources)
    
                Me.PropertiesTableAdapter.Update(C1NWindDataSet.Properties)
    
                Me.CalendarsTableAdapter.Update(C1NWindDataSet.Calendars)
    
            Catch ex As Exception
    
            End Try
        End Sub
    
    
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            LoadData()
    
        End Sub
    
        Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
    
            SaveData()
    
        End Sub
    End Class
    

    C#
    Copy Code
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        
            private void LoadData()
        {
            try
            { 
            this.tasksTableAdapter.Fill(this.c1NWindDataSet.Tasks);
            this.resourcesTableAdapter.Fill(this.c1NWindDataSet.Resources);
            this.propertiesTableAdapter.Fill(this.c1NWindDataSet.Properties);
            this.calendarsTableAdapter.Fill(this.c1NWindDataSet.Calendars); 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    
        private void SaveData()
        {
            try
            {
                this.tasksTableAdapter.Update(c1NWindDataSet.Tasks);
                this.resourcesTableAdapter.Update(c1NWindDataSet.Resources);
                this.propertiesTableAdapter.Update(c1NWindDataSet.Properties);
                this.calendarsTableAdapter.Update(c1NWindDataSet.Calendars);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            LoadData();
           
        }
    
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            SaveData();
        }
        }
    
       

With this, you have configured C1NWind.mdb database to your project and added code to synchronize the data in both the directions.

See Also