ComponentOne FlexPivot for WinForms
Step 2: Connecting FlexPivotPage Control with DataEngine
Quick Start: FlexPivot for WinForms > Using FlexPivot Controls with C1DataEngine > Step 2: Connecting FlexPivotPage Control with DataEngine

In the previous step, you created a basic Windows Forms application and added FlexPivotPage control to it. In this step, you begin with connecting the FlexPivotPage control to C1DataEngine.

To connect the FlexPivotPage control to C1DataEngine, perform the following steps. 

  1. Switch to the code view i.e. Form1.cs and initialize workspace within the form's constructor.
    C1.DataEngine.Workspace.Init(path)
    
    C1.DataEngine.Workspace.Init(path);
    
  2. Initialize full path to the folder where the DataEngine stores data in files, and an SQL connection above the Form's constructor using the following code.
    Private path As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\ComponentOne Samples\Common"
    Private conn As New SqlConnection
    
    string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + @"\ComponentOne Samples\Common";
    SqlConnection conn = new SqlConnection();
    

    Initially, the DataEngine is empty but it automatically fills with files as and when the data is added to it.

  3. Initialize a standard connection string to the database file being used.
    Private Function GetConnectionString() As String
        Dim key As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Microsoft SQL Server Local DB\Installed Versions\12.0")
        Dim ver As String = If(key Is Nothing, "v11.0", "MSSQLLocalDB")
        Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\ComponentOne Samples\Common"
        Return String.Format("Data Source=(LocalDB)\{0};AttachDbFilename={1}\NORTHWND.MDF;Integrated Security=True;Connect Timeout=30", ver, path)
    End Function
    
    static string GetConnectionString()
    {
        Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server Local DB\Installed Versions\12.0");
        string ver = key == null ? "v11.0" : "MSSQLLocalDB";
        string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + @"\ComponentOne Samples\Common";
        return string.Format(@"Data Source=(LocalDB)\{0};AttachDbFilename={1}\C1NWind.mdb;Integrated Security=True;Connect Timeout=30", ver, path);
    }
    
  4. Switch to the Design view and subscribe Form1_Load event from the Properties window. 
  5. Add the following code to the event handler created for Form1_Load event in the code view.  
    Private Sub Form1_Load_1(sender As Object, e As EventArgs) Handles MyBase.Load
        conn.ConnectionString = GetConnectionString()
        conn.Open()
        Dim command = New SqlCommand("Select * from Invoices", conn)
        Dim connector = New C1.DataEngine.DbConnector(conn, command)
        connector.GetData("Invoices")
        C1FlexPivotPage1.FlexPivotPanel.ConnectDataEngine("Invoices")
    End Sub
    
    private void Form1_Load(object sender, EventArgs e)
    {
        conn.ConnectionString = GetConnectionString();
        conn.Open();
        var command = new SqlCommand("Select * from Invoices", conn);
        var connector = new C1.DataEngine.DbConnector(conn, command);
        connector.GetData("Invoices");
        c1FlexPivotPage1.FlexPivotPanel.ConnectDataEngine("Invoices");
    }
    

With this, you have successfully connected the FlexPivotPage control to the DataEngine.