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
- In the project toolbar, select Add New Data Source from the PROJECT menu. The Data Source Configuration Wizard dialog box appears.
- Select Database and then click Next.
- Select Dataset and then click Next.
- Click New Connection
- Select Data source as Microsoft Access Database File
- 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.
- 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.
- Verify the Yes, save the connection as check box is checked and click Next to continue.
The connection string is saved as C1NWindConnectionString.
- Expand the Tables node and select the Categories and Products objects.
- Click Finish.
C1NWindDataSet.xsd is added to your project.
Add an OleDbDataAdapter
- 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.
- 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.
- The Use SQL statements is selected by default, click Next.
- 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
- Click Next and then click Yes to add primary key columns to your query.
- 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:
- Select OleDbDataAdapter1 and click on its smart tag, then click Generate DataSet. The Generate Dataset dialog box appears.
- 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
- From the Toolbox, double-click the OleDbDataAdapter component to add another OleDbDataAdapter component to the component tray.
- Select the data connection that shows the directory, ACCESS. C:\Users\Documents\ComponentOne Samples\Common\C1NWind.mdb from the drop-down listbox.
- Click Next to continue.
- Verify the Use SQL statements is selected and then click Next.
- Copy and paste the following SQL statement in the textbox of the Data Adapter Configuration Wizard:
SELECT CategoryName, CategoryID FROM Categories
- Click Next and then click Finish.
Generate a DataSet for OleDbDataAdapter2
Generate a DataSet that is related to OleDbDataAdapter2 by completing the following steps:
- Select OleDbDataAdapter2 and click on its smart tag, and then click Generate DataSet.
- In the Generate Dataset dialog box, click New, and then name it categoriesDataSet.
- 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:
- 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.
- 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