windowsforms-devexpress-dot-xtracharts-dot-chartcontrol-1481a39c.md
Gets or sets the chart control’s data source.
Namespace : DevExpress.XtraCharts
Assembly : DevExpress.XtraCharts.v25.2.UI.dll
NuGet Package : DevExpress.Win.Charts
public object DataSource { get; set; }
Public Property DataSource As Object
| Type | Description |
|---|---|
| Object |
A Object representing the chart control’s data source.
|
Use the DataSource property to specify the data source from which the current chart control will obtain information about the data series it displays.
Note
For the WebChartControl, the ASPxDataWebControlBase.DataSourceID and ASPxDataWebControlBase.DataSource properties are used, instead.
When a PivotGridControl instance is assigned to the DataSource property, the chart’s bindings and layout are auto-adjusted. To learn more on this, see Pivot Charting (Integration with a Pivot Grid Control).
There are two types of data binding available in a chart control: you can bind either the entire chart, or its individual series. In both cases, you first need to create a data source (any object which implements either IList, IListSource or IBindingList interfaces), and then assign it to the DataSource property.
If series binding is used (when, for series contained within the ChartControl.Series collection, both their SeriesBase.ArgumentDataMember and SeriesBase.ValueDataMembers properties are specified), you can define different data sources for each series using its Series.DataSource property. Note that this property has a higher priority than the DataSource property of the chart control.
Note that data binding performed at the chart control level (chart binding) can force series objects to be created dynamically, based upon the common template settings. For this purpose, you should set the DataSource and ChartControl.SeriesDataMember properties of a chart control and assign the required data fields to the SeriesBase.ArgumentDataMember and SeriesBase.ValueDataMembers properties, which are available via the ChartControl.SeriesTemplate property of the chart control. The auto-created series objects will not exist within the ChartControl.Series collection, and can be customized using the ChartControl.SeriesTemplate settings.
For more information, refer to Providing Data.
The following example binds a chart to data at runtime using series templates. It uses the same approach as the design-time example, but another data table is generated in this code to simplify the example. For this example to work correctly, do not 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 BindUsingTemplatesRuntimeCS {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private DataTable CreateChartData() {
// Create an empty table.
DataTable table = new DataTable("Table1");
// Add three columns to the table.
table.Columns.Add("Month", typeof(String));
table.Columns.Add("Section", typeof(String));
table.Columns.Add("Value", typeof(Int32));
// Add data rows to the table.
table.Rows.Add(new object[] { "Jan", "Section1", 10 });
table.Rows.Add(new object[] { "Jan", "Section2", 20 });
table.Rows.Add(new object[] { "Feb", "Section1", 20 });
table.Rows.Add(new object[] { "Feb", "Section2", 30 });
table.Rows.Add(new object[] { "March", "Section1", 15 });
table.Rows.Add(new object[] { "March", "Section2", 25 });
return table;
}
private void Form1_Load(object sender, EventArgs e) {
// Create a chart.
ChartControl chart = new ChartControl();
// Generate a data table and bind the chart to it.
chart.DataSource = CreateChartData();
// Specify data members to bind the chart's series template.
chart.SeriesDataMember = "Month";
chart.SeriesTemplate.ArgumentDataMember = "Section";
chart.SeriesTemplate.ValueDataMembers.AddRange(new string[] {"Value"});
// Specify the template's series view.
chart.SeriesTemplate.View = new StackedBarSeriesView();
// Specify the template's name prefix.
chart.SeriesNameTemplate.BeginText = "Month: ";
// Dock the chart into its parent, and add it to the current form.
chart.Dock = DockStyle.Fill;
this.Controls.Add(chart);
}
}
}
Imports Microsoft.VisualBasic
Imports System
Imports System.Data
Imports System.Windows.Forms
Imports DevExpress.XtraCharts
' ...
Namespace BindUsingTemplatesRuntimeCS
Partial Public Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
End Sub
Private Function CreateChartData() As DataTable
' Create an empty table.
Dim table As New DataTable("Table1")
' Add three columns to the table.
table.Columns.Add("Month", GetType(String))
table.Columns.Add("Section", GetType(String))
table.Columns.Add("Value", GetType(Int32))
' Add data rows to the table.
table.Rows.Add(New Object() { "Jan", "Section1", 10 })
table.Rows.Add(New Object() { "Jan", "Section2", 20 })
table.Rows.Add(New Object() { "Feb", "Section1", 20 })
table.Rows.Add(New Object() { "Feb", "Section2", 30 })
table.Rows.Add(New Object() { "March", "Section1", 15 })
table.Rows.Add(New Object() { "March", "Section2", 25 })
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()
' Generate a data table and bind the chart to it.
chart.DataSource = CreateChartData()
' Specify data members to bind the chart's series template.
chart.SeriesDataMember = "Month"
chart.SeriesTemplate.ArgumentDataMember = "Section"
chart.SeriesTemplate.ValueDataMembers.AddRange(New String() {"Value"})
' Specify the template's series view.
chart.SeriesTemplate.View = New StackedBarSeriesView()
' Specify the template's name prefix.
chart.SeriesNameTemplate.BeginText = "Month: "
' 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
The following code snippets (auto-collected from DevExpress Examples) contain references to the DataSource property.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.
winforms-visualize-data-grid-rows-in-a-chart/CS/ControlRowSourceSample/MainForm.cs#L19
// Assign a new instance of ControlRowSource to the chart's DataSource property.
chartControl.DataSource = new ControlRowSource() {
Control = mainView,
winforms-charts-sort-stacked-bars-by-total-values-with-qualitativescalecomparer/CS/Form1.cs#L15
InitializeComponent();
chartControl1.DataSource = CreateDataSource();
chartControl1.SeriesDataMember = "Series";
winforms-charts-export-chart-to-xls/CS/Form1.cs#L16
chartControl1.Series.Add(series);
chartControl1.DataSource = GetSales();
series.ArgumentDataMember = "Region";
winforms-charts-export-chart-to-pdf/CS/Form1.cs#L17
chartControl1.Series.Add(series);
chartControl1.DataSource = GetSales();
series.ArgumentDataMember = "Region";
// Define series template for multiple series.
chart.DataSource = GetSeriesTemplateData();
chart.SeriesDataMember = "Region";
winforms-visualize-data-grid-rows-in-a-chart/VB/ControlRowSourceSample/MainForm.vb#L21
' Assign a new instance of ControlRowSource to the chart's DataSource property.
chartControl.DataSource = New ControlRowSource() With {.Control = mainView, .ControlRows = ControlRows.Selected}
' Specify chart properties that configures series to display.
winforms-charts-sort-stacked-bars-by-total-values-with-qualitativescalecomparer/VB/Form1.vb#L16
InitializeComponent()
chartControl1.DataSource = CreateDataSource()
chartControl1.SeriesDataMember = "Series"
winforms-charts-export-chart-to-xls/VB/Form1.vb#L19
chartControl1.Series.Add(series)
chartControl1.DataSource = GetSales()
series.ArgumentDataMember = "Region"
winforms-charts-export-chart-to-pdf/VB/Form1.vb#L20
chartControl1.Series.Add(series)
chartControl1.DataSource = GetSales()
series.ArgumentDataMember = "Region"
' Define series template for multiple series.
chart.DataSource = GetSeriesTemplateData()
chart.SeriesDataMember = "Region"
See Also