Category / Section
How to bind data table in WPF Chart (SfChart)?
1 min read
The DataTable type can be bound to the ItemsSource property in WPF Chart (SfChart).
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; } } }