)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to bind data table in the SfChart?

Platform: UWP |
Control: SfChart |
Published Date: January 27, 2016 |
Last Revised Date: June 27, 2019 |
.NET Framework: 4.5.1 |
Product Version: 12.4.0.24

Description:

The DataTable type can be bound to the ItemsSource property in ChartSeries.

Solution:

The following code example illustrates how to bind the data tables in ChartSeries.

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

Chart rendered by getting data from DataTable in WPF

Figure 1: ColumnSeries renders based on data table fields.

2X faster development

The ultimate UWP UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon