Visual Studio 2010 – The Windows Forms is not death – The Chart Control
Bosanska verzija teksta može se pročitati ovdje.
With releasing the WPF platform, there are good reasons and questions of what will happen with WindowsForms platform.The official said, for this platform are promising and there is no concern, and it will be continue to develop. Needles to say that the WindowsForms is the most common platform. The News coming from the new version of VS 2010 confirm this fact, and this post will be exactly related to new features in WindowsForms. With the new version of Visual Studio 2010 there is a new control: Char Control .
This control is located in the System.Windows.Forms.DataVisualization namespace. Looking thought the ObjectBrowser, there is only one control. Probably they will be more in the future.
Chart control has support for connecting to external data source, which means that we can use and connect it directly to database, through one of the existing ORM maper (DataSet, LINQtoSQL or EF). In this post, we will make a simple example with applying the Chart control.
At the beginning let’s create Windows Forms application via File-> New -> Project.
Create EF model for the Northwind database, with standard tables: Orders and Order_Details.
In fact, when you open the Form1 and show the DataSources window, our tables are shown, but the designer does not allow us to drag and drop them in to the form (see picture below).
In order to use the DataSources designer window, we need to add these tables as objects of the DataSources. For this purpose choose Add New DataSource button, then choose Object item and choose Next button (see picture below).
Then in the next window, select our Tables and finish the wizard.
DataSources window now looks like the following picture:
We got two new items that can drag on Form1. Drag and drop the DataGrid objects in Form1, and add Chart control from ToolBox as shown in the following picture:
Since the DataGrid controls are already connected to bindingsource, it is now necessary to connect the Chart control with Order_Details table. To show data in the graph we need associate columns Quantity and UnitPrice with the chart control members.
To achieve this, it is neccessery chart1.Datasource apply with orders_detailsBidningSource.
When we apply the data source to the chart control, we are now able to set how data will be presented.Chart control has a Series property, and within Series property you can find properties for displaying data in X and Y axes (see picture below).
With this we finished setting up Chart control.
It is now necessary to load data from the database. Open the Form1.cs file, implement the events Form_Load and ListChanged, similar on the picture below.
We see that we have set up two events for the Form1 Load event and ListChanged event for order_DetailsBidnigSource. The second event is necessary to refresh the Chart control.
If you run the project, the result is similar to the following picture.
When we change the current row Order table, changes the content is below the table, as well as in the chart control. This example, we implemented Master-Detail relationship with two DataGrid and one Chart control.