How to bind data table in WPF Chart (SfChart)?
Description
In WPF SfChart, you can bind a DataTable as the data source for the chart series. This allows seamless virtualization of data retrieved from databases.
Solution
The DataTable type can be bound to the ItemsSource property. The chart series can access rows in the DataTable using the ItemsSource property, with the XBindingPath and YBindingPath properties defining the appropriate data columns. Refer to the following code snippets.
XAML
<Grid x:Name="grid"> <Grid.DataContext> <local:DataViewModel/> </Grid.DataContext>22 <chart:SfChart> <chart:ColumnSeries ItemsSource="{Binding Data}" XBindingPath="ProductName" YBindingPath="UnitsInStock"></chart:ColumnSeries> </chart:SfChart> </Grid>
C#
public class DataViewModel { DataSet dataset = new DataSet(); public DataViewModel() { AddData(); } public void AddData() { //Sets Database connection. string connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}", @"..\..\Model\DataBase_File.mdb"); OleDbConnection Connection = new OleDbConnection(connectionString); Connection.Open(); OleDbCommand command = new OleDbCommand("Select Top 10 * from [Products]", Connection); OleDbDataAdapter DataAdapter = new OleDbDataAdapter(command); DataAdapter.Fill(dataset, "Product"); //Sets the DataTable to Data property. this.Data = dataset.Tables["Product"]; Connection.Close(); } public DataTable _data; public DataTable Data { get { return _data; } set { _data = value; } } }
Output
Conclusion
I hope you enjoyed learning about how to bind data table in WPF Chart (SfChart).
You can refer to our WPF Chart feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WPF Chart example 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!