How to bind a dataset to the WinForms Chart?
In this guide, we will demonstrate how to bind data from a database using the ChartDataBindModel class in WinForms Chart. We will cover setting the data source, configuring the XName and YNames properties, and using the ChartDataBindAxisLabelModel class to load X-axis labels from the database.
C#
//Custom Dataset bound to the Demographics table
DataSet dataSet1 = new DataSet("DataSet1");
DataTable demographicsTable = new DataTable("Demographics");
demographicsTable.Columns.Add("ID", typeof(int));
demographicsTable.Columns.Add("City", typeof(string));
demographicsTable.Columns.Add("Population", typeof(int));
demographicsTable.Rows.Add(1, "Chennai", 8000000);
demographicsTable.Rows.Add(2, "Mumbai", 12400000);
demographicsTable.Rows.Add(3, "Delhi", 11000000);
demographicsTable.Rows.Add(4, "Bangalore", 8500000);
demographicsTable.Rows.Add(5, "Hyderabad", 6800000);
demographicsTable.Rows.Add(6, "Kolkata", 4500000);
dataSet1.Tables.Add(demographicsTable);
ChartDataBindModel model = new ChartDataBindModel(dataSet1, "Demographics");
//Column that contains the X values
model.XName = "ID";
//Column that contains the Y values
model.YNames = new string[] { "Population" };
//Configure the chart series
ChartSeries series = new ChartSeries();
series.Type = ChartSeriesType.Line;
series.SeriesModelImpl = model;
this.chartControl1.Series.Add(series);
//The columns that has the label values corresponding X values
ChartDataBindAxisLabelModel xAxisLabelModel = new ChartDataBindAxisLabelModel(dataSet1, "Demographics");
xAxisLabelModel.LabelName = "City";
this.chartControl1.PrimaryXAxis.LabelsImpl = xAxisLabelModel;
this.chartControl1.PrimaryXAxis.ValueType = ChartValueType.Custom;
VB
'Custom Dataset bound to Demographics tables
Dim dataset11 As New DataSet("Dataset1")
Dim demographicstable As New DataTable("Demographics")
demographicstable.Columns.Add("ID", GetType(Integer))
demographicstable.Columns.Add("City", GetType(String))
demographicstable.Columns.Add("Population", GetType(Integer))
demographicstable.Rows.Add(1, "Chennai", 8000000)
demographicstable.Rows.Add(2, "Mumbai", 12400000)
demographicstable.Rows.Add(3, "Delhi", 11000000)
demographicstable.Rows.Add(4, "Bangalore", 8500000)
demographicstable.Rows.Add(5, "Hyderabad", 6800000)
demographicstable.Rows.Add(6, "Kolkata", 4500000)
dataset11.Tables.Add(demographicstable)
Dim model As New ChartDataBindModel(dataset11, "Demographics")
'Column that contains the X values
model.XName = "ID"
'Column that contains the Y values
model.YNames = New String() {"Population"}
'Configure the chart series
Dim series As ChartSeries = Me.lineChart.Model.NewSeries("data bound series")
series.Type = ChartSeriesType.Line
series.SeriesModelImpl = model
lineChart.Series.Add(series)
'The columns that has the label values corresponding X values
Dim xaxislabelmodel = New ChartDataBindAxisLabelModel(dataset11, "Demographics")
xaxislabelmodel.LabelName = "City"
lineChart.PrimaryXAxis.LabelsImpl = xaxislabelmodel
lineChart.PrimaryXAxis.ValueType = ChartValueType.Custom
This is illustrated in the Chart UG in this topic: DataBinding
Output:
Conclusion
I hope you enjoyed learning about how to bind a dataset to the WinForms Chart.
You can refer to our WinForms Chart feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. You can also explore our WinForms Chart examples to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!