FlexPivot for WPF | ComponentOne
In This Topic
    Binding FlexPivot
    In This Topic

    You can populate FlexPivot with data by binding the FlexPivotPage control with a data source. For this, you can use a data table or a data engine as the data source for binding to the FlexPivot. Let us discuss how you can bind the FlexPivot to a data table and a data engine in the following sections.

    Bind FlexPivot to DataTable

    To bind the FlexPivot to a data table, follow these steps:

    1. Establish a connection to the C1NWind database using the OLE DB provider connection string, as shown in the following code:
      C#
      Copy Code
      //Establish connection to database and fetch data
      OleDbConnection oconn = new OleDbConnection();
      oconn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=..\..\Documents\ComponentOne Samples\Common\C1NWind.mdb";
      var da = new OleDbDataAdapter("select * from Invoices", oconn);
      
    2. Fetch the data from the database to the data table using the following code:
      C#
      Copy Code
      var dt = new DataTable();
      da.Fill(dt);
      
    3. Bind the datatable to FlexPivot's C1PivotEngine using the FlexPivotPanel.C1PivotEngine property as shown in the following code:
      C#
      Copy Code
      flexPivotPage.FlexPivotPanel.C1PivotEngine.BeginUpdate();
      flexPivotPage.DataSource = dt.DefaultView;
      flexPivotPage.FlexPivotPanel.C1PivotEngine.EndUpdate();
      

    Bind FlexPivot to DataEngine

    To bind the FlexPivot to a data table, follow these steps:

    1. Establish a connection to the C1NWind database using the OLE DB provider connection string, as shown in the following code:
      C#
      Copy Code
      //Establish connection to database and fetch data
      OleDbConnection oconn = new OleDbConnection();
      oconn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=..\..\Documents\ComponentOne Samples\Common\C1NWind.mdb";
      var da = new OleDbDataAdapter("select * from Invoices", oconn);
      
    2. Fetch the data from the database to the data table using the following code:
      C#
      Copy Code
      var dt = new DataTable();
      da.Fill(dt);
      dt.TableName = "Invoices";
      
    3. Initialize the DataEngine workspace for storing DataEngine data using the following code:
      C#
      Copy Code
      string dataPath = Path.Combine(Directory.GetCurrentDirectory(), "Data");
      //Storing DataEngine data
      flexPivotPage.FlexPivotPanel.Workspace.Init(dataPath);
      BindToDataEngine();
      
    4. Import the data from the data table to the DataEngine's datatable using the following code:
      C#
      Copy Code
      //Import data in DataEngine datatable
      C1.DataEngine.DbConnector.GetData(flexPivotPage.FlexPivotPanel.Workspace, dt.CreateDataReader(), dt.TableName);
      
    5. Bind the datatable to FlexPivot's C1PivotEngine using the FlexPivotPanel.C1PivotEngine property as shown in the following code:
      C#
      Copy Code
      //Connect FlexPivot to DataEngine filled with data
      var fPanel = flexPivotPage.FlexPivotPanel;
      fPanel.C1PivotEngine.BeginUpdate();
      fPanel.ConnectDataEngine(dt.TableName);
      fPanel.C1PivotEngine.EndUpdate();