Skip to main content
A newer version of this page is available. .

How to: Bind a Dashboard to a DataSet Populated from an XML File

  • 2 minutes to read

To learn how to bind a dashboard to the Object data source using the Dashboard Designer, see Binding to Object Data Sources.

This example demonstrates how to use the DashboardDesigner.DataLoading event to load the data from XML file at runtime.

It contains a dashboard bound to an object data source with fake data whose structure is defined by an XML schema. The actual data is loaded at runtime from an XML file.

To load the data, click the Load Data custom button in the dashboard title. The button is created by handling the CustomizeDashboardTitle event. Its click action calls the DashboardViewer.ReloadData method that triggers the DashboardViewer.DataLoading event. The actual data is obtained within the event handler and assigned to the e.Data property.

Note

The complete sample project How to Load XML Data from a File at Runtime is available in the DevExpress Examples repository.

using DevExpress.DashboardCommon;
using DevExpress.DashboardWin;
using DevExpress.XtraEditors;
using System;
using System.Data;

namespace Dashboard_DataLoading_Example
{
    public partial class Form1 : XtraForm
    {

        string dataFileName = string.Empty;
        public Form1()
        {
            InitializeComponent();
            dashboardViewer1.CustomizeDashboardTitle += DashboardViewer1_CustomizeDashboardTitle;
            dashboardViewer1.Dashboard = new Dashboard1();
            dashboardViewer1.DataLoading += DashboardViewer1_DataLoading;
        }

        private void DashboardViewer1_DataLoading(object sender, DataLoadingEventArgs e)
        {
            if (e.DataSourceName == "EnergyConsumptionBySector")
            {
                e.Data = GetData(dataFileName);
            }
        }

        public DataTable GetData(string fileName)
        {
            if (fileName == string.Empty) return null;

            DataSet xmlDataSet = new DataSet();
            xmlDataSet.ReadXml(fileName);
            return xmlDataSet.Tables["CountriesBySector"];
        }

        private void DashboardViewer1_CustomizeDashboardTitle(object sender, CustomizeDashboardTitleEventArgs e)
        {
            DashboardToolbarItem titleButton = new DashboardToolbarItem("Load Data",
                new Action<DashboardToolbarItemClickEventArgs>((args) =>
                {
                    LoadNewData();
                }));
            titleButton.Caption = "Load Data";
            e.Items.Add(titleButton);
        }
        private void LoadNewData()
        {
            dataFileName = @"..\..\Data\DashboardEnergyConsumptionBySector.xml";
            dashboardViewer1.ReloadData();
        }
    }
}
See Also