windowsforms-3095-controls-and-libraries-chart-control-examples-creating-charts-providing-data-how-to-bind-individual-chart-series-to-data-runtime-sample.md
The following example demonstrates how to bind a chart to data at runtime via binding its individual series to a particular data source. It uses the same approach as the design-time example, but another data table is generated in this code to simplify the example.
Note
Don’t forget to include all necessary assemblies to the References list of your project.
using System;
using System.Data;
using System.Windows.Forms;
using DevExpress.XtraCharts;
// ...
namespace BindIndividualSeriesRuntimeCS {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private DataTable CreateChartData(int rowCount) {
// Create an empty table.
DataTable table = new DataTable("Table1");
// Add two columns to the table.
table.Columns.Add("Argument", typeof(Int32));
table.Columns.Add("Value", typeof(Int32));
// Add data rows to the table.
Random rnd = new Random();
DataRow row = null;
for (int i = 0; i < rowCount; i++) {
row = table.NewRow();
row["Argument"] = i;
row["Value"] = rnd.Next(100);
table.Rows.Add(row);
}
return table;
}
private void Form1_Load(object sender, EventArgs e) {
// Create a chart.
ChartControl chart = new ChartControl();
// Create an empty Bar series and add it to the chart.
Series series = new Series("Series1", ViewType.Bar);
chart.Series.Add(series);
// Generate a data table and bind the series to it.
series.DataSource = CreateChartData(50);
// Specify data members to bind the series.
series.ArgumentScaleType = ScaleType.Numerical;
series.ArgumentDataMember = "Argument";
series.ValueScaleType = ScaleType.Numerical;
series.ValueDataMembers.AddRange(new string[] { "Value" });
// Set some properties to get a nice-looking chart.
((SideBySideBarSeriesView)series.View).ColorEach = true;
((XYDiagram)chart.Diagram).AxisY.Visibility = DevExpress.Utils.DefaultBoolean.False;
chart.Legend.Visibility = DevExpress.Utils.DefaultBoolean.False;
// Dock the chart into its parent and add it to the current form.
chart.Dock = DockStyle.Fill;
this.Controls.Add(chart);
}
}
}
Imports System
Imports System.Data
Imports System.Windows.Forms
Imports DevExpress.XtraCharts
' ...
Namespace BindIndividualSeriesRuntimeCS
Partial Public Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
End Sub
Private Function CreateChartData(ByVal rowCount As Integer) As DataTable
' Create an empty table.
Dim table As New DataTable("Table1")
' Add two columns to the table.
table.Columns.Add("Argument", GetType(Int32))
table.Columns.Add("Value", GetType(Int32))
' Add data rows to the table.
Dim rnd As New Random()
Dim row As DataRow = Nothing
For i As Integer = 0 To rowCount - 1
row = table.NewRow()
row("Argument") = i
row("Value") = rnd.Next(100)
table.Rows.Add(row)
Next i
Return table
End Function
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
' Create a chart.
Dim chart As New ChartControl()
' Create an empty Bar series and add it to the chart.
Dim series As New Series("Series1", ViewType.Bar)
chart.Series.Add(series)
' Generate a data table and bind the series to it.
series.DataSource = CreateChartData(50)
' Specify data members to bind the series.
series.ArgumentScaleType = ScaleType.Numerical
series.ArgumentDataMember = "Argument"
series.ValueScaleType = ScaleType.Numerical
series.ValueDataMembers.AddRange(New String() { "Value" })
' Set some properties to get a nice-looking chart.
CType(series.View, SideBySideBarSeriesView).ColorEach = True
CType(chart.Diagram, XYDiagram).AxisY.Visibility = DevExpress.Utils.DefaultBoolean.False
chart.Legend.Visibility = DevExpress.Utils.DefaultBoolean.False
' Dock the chart into its parent and add it to the current form.
chart.Dock = DockStyle.Fill
Me.Controls.Add(chart)
End Sub
End Class
End Namespace
How to: Bind Individual Chart Series to Data at Design TimeThis tutorial explains how to bind each Chart Series to an individual data source at design time.How to: Bind Chart Series to Data in the Data Source Wizard This tutorial describes how to bind a chart to an external data source in the Data Source Configuration Wizard.How to: Specify Series Data MembersThis tutorial describes how to specify a data source for a created series, and populate series points automatically based on the data source table. See Also