New to Telerik UI for WinFormsStart a free 30-day trial

Populate RadChartView from Access Database

Updated over 6 months ago

Environment

Product VersionProductAuthor
2022.3.913RadChartView for WinFormsDinko Krastev

Description

RadChartView provides two ways of populating with data:

Using one of the two approaches you can populate the chart with the data extracted from the database. In this tutorial, we are going to get the data from the Access Database file and create DataTable to populate the RadChartView.

Solution

Here is a screenshot of how our Access Database could look like.

Sample Access Database

What we need to do is to extract the table values from the Access Database. Then we can create DataTable and use it as a DataSource for the RadChartView.

ChartView Access Database

C#
public partial class Form1 : Form
{
    DataTable table;
    public Form1()
    {
        InitializeComponent();
        table = new DataTable();
        table.Columns.Add("Val_PLC_HMI", typeof(double));
        table.Columns.Add("VAL_PLC_EXT", typeof(double));
        table.Columns.Add("Data_ora", typeof(DateTime));
        PopulateChart();
    }
    void PopulateChart()
    {
        OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=../../Archivio.mdb");
        connection.Open();
        OleDbDataReader reader = null;

        // Check_Vals will be your table name
        OleDbCommand command = new OleDbCommand("SELECT * FROM Check_Vals", connection);
        reader = command.ExecuteReader();

        while (reader.Read())
        {
            var Val_PLC_HMI = double.Parse(reader[1].ToString());
            var VAL_PLC_EXT = double.Parse(reader[2].ToString());
            var Data_ora = DateTime.Parse(reader[3].ToString());
            table.Rows.Add(Val_PLC_HMI, VAL_PLC_EXT, Data_ora);
        }

        connection.Close();

        DateTimeContinuousAxis dateTimeContinuousAxis = new DateTimeContinuousAxis();
        dateTimeContinuousAxis.MajorStepUnit = TimeInterval.Hour;

        dateTimeContinuousAxis.MajorStep = 1;
        dateTimeContinuousAxis.LabelFormat = "{0:hh:mm}";
        LineSeries lineVal_PLC_HMI = new LineSeries();
        lineVal_PLC_HMI.HorizontalAxis = dateTimeContinuousAxis;
        radChartView1.Series.Add(lineVal_PLC_HMI);

        lineVal_PLC_HMI.ValueMember = "Val_PLC_HMI";
        lineVal_PLC_HMI.CategoryMember = "Data_ora";
        lineVal_PLC_HMI.DataSource = table;
    }
}
     
       
In this article
EnvironmentDescriptionSolution
Not finding the help you need?
Contact Support