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 clip_image002.

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.

image

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.

image

Create EF model for the Northwind database, with standard tables: Orders and Order_Details.

image

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).

image

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).

image

Then in the next window, select our Tables and finish the wizard.

image

DataSources window now looks like the following picture:

image

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:

image

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.

image

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).

image

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.

image

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.

image

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.


About these ads

About Bahrudin Hrnjica

Senior Software Developer at daenet, Microsoft MVP for C#, C , WPF, Silverlight, WP7, Math, Mechanical Engineering, Evolutionary Algorithms, Blogger.

Posted on 23/10/2009, in .NET, CodeProject and tagged , , , . Bookmark the permalink. 4 Comments.

  1. Thank you for those instructions but I have an issue. I’ve been looking to use this control since a long time for a university project but unfortunately I don’t see this control in the toolbox; and I mean anywhere in the toolbox (I’ve inspected every section). How can I make it appear. All the chart controls I have found online relate to VS2008. I am using VS2010.

  2. I am no professional prarmomger, but as an amater I wrote a blog post () where, as the title suggest, I recommended 9 programming books. And our intersection is 0. Odd! But eyeing your list, it is pretty clear we have different coding interests . Although I have to admit that 2 or 3 of the books you highlight seem pretty interesting!Ruben

  1. Pingback: GPdotNET with new chart control « Bahrudin Hrnjica Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 409 other followers

%d bloggers like this: