Series.DataSource Property

Gets or sets the series' data source.

Namespace: DevExpress.XtraCharts

Assembly: DevExpress.XtraCharts.v20.1.dll

Declaration

[XtraChartsLocalizableCategory(XtraChartsCategory.Data)]
public object DataSource { get; set; }
<XtraChartsLocalizableCategory(XtraChartsCategory.Data)>
Public Property DataSource As Object

Property Value

Type Description
Object

A Object representing the series' data source.

Remarks

Use the DataSource property to specify the data source from which the current series will obtain information about the data points it displays.

The DataSource property is useful for series binding to bind each series separately. When binding an individual series object, the DataSource property of the series has a higher priority than the ChartControl.DataSource property of a chart control. For instance, if a chart control is bound to the dataView1 object via its ChartControl.DataSource property and a particular series of the chart control is bound to the dataView2 object via its DataSource property, the series will contain data points obtained from the dataView2 object according to the SeriesBase.ArgumentDataMember and SeriesBase.ValueDataMembers property settings of the series.

If the DataSource property of a series is not set (its value is null) but the chart control that contains this series is bound to data via its ChartControl.DataSource property, the series' SeriesBase.ArgumentDataMember and SeriesBase.ValueDataMembers properties can be defined so that the series will obtain data point information from the corresponding data fields of the chart control's data source.

For more information, refer to Providing Data.

Examples

The following example demonstrates how to bind a chart to data at runtime via binding its individual series to a particular datasource. 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);
        }
    }
}
See Also