2D Chart for WinForms | ComponentOne
Chart for WinForms Quick Start / Step 1 of 4: Create a Data Source for Chart
In This Topic
    Step 1 of 4: Create a Data Source for Chart
    In This Topic

    In this step you will create a data source that you can later bind the chart to using the Chart Properties designer. Create a .NET project, and complete the following steps:

    Add a new data source

    1. In the project toolbar, select Add New Data Source from the PROJECT menu. The Data Source Configuration Wizard dialog box appears.
    2. Select Database and then click Next.
    3. Select Dataset and then click Next.
    4. Click New Connection
    5. Select Data source as Microsoft Access Database File
    6. Click browse for adding Database file name as C1NWind.mdb. Navigate to the default location of C1NWind.mdb database (located by default in C:\Users\Documents\ComponentOne Samples\Common), click Open, and then click OK
    7. Click the Next button to continue. A dialog box appears asking if you would like to add the data file to your project and modify the connection string. Since it is not necessary to copy the database to your project, click No.
    8. Verify the Yes, save the connection as check box is checked and click Next to continue.

      The connection string is saved as C1NWindConnectionString.

    9. Expand the Tables node and select the Categories and Products objects.
    10. Click Finish.

      C1NWindDataSet.xsd is added to your project.

    Add an OleDbDataAdapter

    1. From the Toolbox, double-click the OleDbDataAdapter component.
      Note: In Visual Studio, right-click the Toolbox, and then click Choose Items. On the .NET Framework Components tab in the dialog box, select OleDbDataAdapter.

      The OleDbDataAdapter appears in the component tray and the Data Adapter Configuration Wizard appears.

    2. In the Data Adapter Configuration Wizard, choose the connection you wish to use for the data adapter from the drop-down listbox (in this case, C:\Users\Documents\ComponentOne Samples\Common\C1NWind.mdb) and then click Next.
    3. The Use SQL statements is selected by default, click Next.
    4. Copy and paste the following SQL statement in the textbox of the Data Adapter Configuration Wizard:
      SELECT CategoryID, ProductName, UnitPrice, UnitsInStock, ReorderLevel FROM Products ORDER BY UnitPrice DESC
    5. Click Next and then click Yes to add primary key columns to your query.
    6. Click Finish.

      Notice that the OleDbConnection1 component is automatically inserted in the component tray.

      Generate a DataSet

      Generate a DataSet that is related to OleDbDataAdapter1 by completing the following steps:

    7. Select OleDbDataAdapter1 and click on its smart tag, then click Generate DataSet. The Generate Dataset dialog box appears.
    8. Verify that the Existing radio button is selected, the Products table is selected, and the option Add this dataset to the designer is selected, and then click OK.

      The C1NWindDataSet1 is added to the component tray.

    Add a second OleDbDataAdapter

    1. From the Toolbox, double-click the OleDbDataAdapter component to add another OleDbDataAdapter component to the component tray.
    2. Select the data connection that shows the directory, ACCESS. C:\Users\Documents\ComponentOne Samples\Common\C1NWind.mdb from the drop-down listbox.
    3. Click Next to continue.
    4. Verify the Use SQL statements is selected and then click Next.
    5. Copy and paste the following SQL statement in the textbox of the Data Adapter Configuration Wizard:
      SELECT CategoryName, CategoryID FROM Categories
    6. Click Next and then click Finish.

    Generate a DataSet for OleDbDataAdapter2

    Generate a DataSet that is related to OleDbDataAdapter2 by completing the following steps:

    1. Select OleDbDataAdapter2 and click on its smart tag, and then click Generate DataSet.
    2. In the Generate Dataset dialog box, click New, and then name it categoriesDataSet.
    3. Verify that the Categories table is selected and that the option Add this dataset to the designer is selected, and then click OK.

      The categoriesDataSet1 is added to the component tray.

    Fill the datasets

    To fill the DataSets, add the following code in the Form1_Load event:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    oleDbDataAdapter1.Fill(c1nwindDataSet1)
    oleDbDataAdapter2.Fill(categoriesDataSet1)
    

    To write code in C#

    C#
    Copy Code
    oleDbDataAdapter1.Fill(c1nwindDataSet1);
    oleDbDataAdapter2.Fill(categoriesDataSet1);
    

    Add the DataView component

    Return to Design view, and complete the following steps:

    1. From the Toolbox, double-click the DataView component to add it to the component tray.
      Note: In Visual Studio, right-click the Toolbox, and then click Choose Items. On the .NET Framework Components tab in the dialog box, select DataView.
    2. In the DataView Properties window set the properties to the following:
      • AllowDelete = False
      • AllowEdit = False
      • AllowNew = False
      • Table = c1nwindDataSet1.Products

    Congratulations! You have successfully created a data source. The next step will show you how to add a chart and bind it to the existing data source as well as how to easily customize your chart using the Chart Properties designer.

    See Also