# Binding FlexPivot to Data Source in Code

This topic covers information about binding flexpivot in code.

## Content

You can also bind the FlexPivot controls to a data source kept locally at your machine in code. Follow the given steps to bind the FlexPivotPage control to data source (c1nwind.mdb) in code.

### In Code

1. Create a Windows Forms Application project in Visual Studio.
2. Drag-and-drop [FlexPivotPage](/componentone/api/win/online-flexpivot/dotnet-api/C1.Win.FlexPivot.10/C1.Win.FlexPivot.FlexPivotPage.html) control to the Form.
3. Switch to the code view and add the following code to initialize a standard connection string.

    ```vbnet
    Private Shared Function GetConnectionString() As String
        Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\ComponentOne Samples\Common"
        Dim conn As String = "provider=microsoft.jet.oledb.4.0;data source={0}\c1nwind.mdb;"
        Return String.Format(conn, path)
    End Function
    ```

    ```csharp
    static string GetConnectionString()
    {
        string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + @"\ComponentOne Samples\Common";
        string conn = @"provider=microsoft.jet.oledb.4.0;data source={0}\c1nwind.mdb;";
        return string.Format(conn, path);
    }
    ```

    This code creates a connection with the **c1nwind.mdb** database file installed on your system at **Documents\\ComponentOne Samples\\Common** location on your system.
4. Add the following code in the Form's constructor to fetch data from the data source through Oledb adapters.

    ```vbnet
    ' load data
    Dim da = New OleDbDataAdapter("select * from invoices", GetConnectionString())
    Dim dt = New DataTable()
    da.Fill(dt)
    ```

    ```csharp
    // load data
    var da = new OleDbDataAdapter("select * from invoices", GetConnectionString());
    var dt = new DataTable();
    da.Fill(dt);
    ```
5. Bind the FlexPivotPage control to the data source using [DataSource](/componentone/api/win/online-flexpivot/dotnet-api/C1.Win.FlexPivot.10/C1.Win.FlexPivot.FlexPivotPage.DataSource.html) property of [FlexPivotPage](/componentone/api/win/online-flexpivot/dotnet-api/C1.Win.FlexPivot.10/C1.Win.FlexPivot.FlexPivotPage.html) class in the Form's constructor.

    ```vbnet
    ' bind data
    Me.FlexPivotPage1.DataSource = dt
    ```

    ```csharp
    // bind data
    this.flexPivotPage1.DataSource = dt;
    ```
6. Press **F5** to run the application. FlexPivotPage is now connected to the c1nwind.mdb file with various data fields available in the FlexPivotPanel.
<br>
    ![FlexPivot_view](https://cdn.mescius.io/document-site-files/images/78f24d1c-0e57-452f-87a1-d318e186587a/images/flexpivot_view.png)

You can now create different views by dragging the data fields to Rows, Columns and Values list for data analysis.