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

Bind to XPO Data Sources

  • 6 minutes to read

Instant Feedback

In this example, the GridControl is bound to an XPInstantFeedbackSource object. This object activates an Instant Feedback UI Mode. In this mode, you will no longer experience any UI freezing. Data operations are performed asynchronously in a background thread. The GridControl and your application are always highly responsive.

View Example

<Window x:Class="InstantFeedbackMode.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
        xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core">
    <Grid>
        <dxg:GridControl Name="grid" AutoGenerateColumns="AddNew" />
    </Grid>
</Window>
using System;
using System.Windows;
using DevExpress.Data.Filtering;
using DevExpress.Xpf.Grid;
using DevExpress.Xpo;
using DevExpress.Xpf.Core;

namespace InstantFeedbackMode {
    public partial class MainWindow : Window {

        public MainWindow() {
            InitDAL();
            // Generates test data - 100,000 records.
            GenerateTestData(100000);
            InitializeComponent();

            // Creates and initializes the data source which activates the Instant Feedback UI Mode.
            XPInstantFeedbackSource instantDS = new XPInstantFeedbackSource(typeof(TestObject));
            instantDS.ResolveSession += new EventHandler<ResolveSessionEventArgs>(instantDS_ResolveSession);
            instantDS.DismissSession += new EventHandler<ResolveSessionEventArgs>(instantDS_DismissSession);

            // Bind the grid.
            grid.ItemsSource = instantDS;
        }

        void instantDS_ResolveSession(object sender, ResolveSessionEventArgs e) {
            e.Session = new UnitOfWork();
        }

        void instantDS_DismissSession(object sender, ResolveSessionEventArgs e) {
            IDisposable session = e.Session as IDisposable;
            if (session != null) {
                session.Dispose();
            }
        }

        private static void InitDAL() {
            XpoDefault.Session = null;
            XpoDefault.DataLayer = XpoDefault.GetDataLayer(
                DevExpress.Xpo.DB.AccessConnectionProvider.GetConnectionString("test.mdb"),
                DevExpress.Xpo.DB.AutoCreateOption.DatabaseAndSchema
                );
        }

        private void GenerateTestData(int recordCount) {
            using (UnitOfWork s = new UnitOfWork()) {
                if (s.FindObject<TestObject>(null) != null) return;
                for (int i = 0; i < recordCount; i++) {
                    TestObject o = new TestObject(s) { HasAttachment = false, Sent = DateTime.Now, Subject = string.Format("Subject {0}", i) };
                }
                s.CommitChanges();
            }
        }
    }
}
using System;
using DevExpress.Xpo;

namespace InstantFeedbackMode {
    public class TestObject : XPObject {
        string _subject;
        public string Subject {
            get { return _subject; }
            set { SetPropertyValue<string>("Subject", ref _subject, value); }
        }
        DateTime _sent;
        public DateTime Sent {
            get { return _sent; }
            set { SetPropertyValue<DateTime>("Sent", ref _sent, value); }
        }

        bool _hasAttachment;
        public bool HasAttachment {
            get { return _hasAttachment; }
            set { SetPropertyValue<bool>("HasAttachment", ref _hasAttachment, value); }
        }
        public TestObject(Session session) : base(session) { }
        public TestObject() : base(Session.DefaultSession) { }
        public override void AfterConstruction() { base.AfterConstruction(); }
    }
}

Server Mode

This example shows how to provide the GridControl with data using the eXpress Persistent Objects (XPO).

  1. Create a new persistent class representing a data object TestObject:

    using System;
    using DevExpress.Xpo;
    
    namespace XpoServMode {
        public class TestObject : XPObject {
            string _subject;
            public string Subject {
                get { return _subject; }
                set { SetPropertyValue<string>("Subject", ref _subject, value); }
            }
            DateTime _sent;
            public DateTime Sent {
                get { return _sent; }
                set { SetPropertyValue<DateTime>("Sent", ref _sent, value); }
            }
    
            bool _hasAttachment;
            public bool HasAttachment {
                get { return _hasAttachment; }
                set { SetPropertyValue<bool>("HasAttachment", ref _hasAttachment, value); }
            }
            public TestObject(Session session) : base(session) { }
            public TestObject() : base(Session.DefaultSession) { }
            public override void AfterConstruction() { base.AfterConstruction(); }
        }
    }
    
  2. Define a database to store data.

  3. Bind the GridControl to a new XPCollection object:

    <Window
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:XpoServMode"
            xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" x:Class="XpoServMode.MainWindow"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800">
            <dxg:GridControl Name="grid" AutoGenerateColumns="AddNew" EnableSmartColumnsGeneration="True">
                <dxg:GridControl.TotalSummary>
                    <dxg:GridSummaryItem Alignment="Left" FieldName="Oid" SummaryType="Count"/>
                </dxg:GridControl.TotalSummary>
                <dxg:GridControl.View>
                    <dxg:TableView ShowFixedTotalSummary="True"/>
                </dxg:GridControl.View>
            </dxg:GridControl>
    </Window>
    
    using DevExpress.Xpo;
    using System;
    using System.Windows;
    
    namespace XpoServMode {
        public partial class MainWindow : Window {
            public MainWindow() {
                InitDAL();
                GenerateTestData(100000);
                InitializeComponent();
    
                var session = new UnitOfWork(XpoDefault.DataLayer);
                var serverDS = new XPServerCollectionSource(session, typeof(TestObject));
                grid.ItemsSource = serverDS;
            }
    
            private static void InitDAL() {
                XpoDefault.Session = null;
                XpoDefault.DataLayer = XpoDefault.GetDataLayer(
                    DevExpress.Xpo.DB.AccessConnectionProvider.GetConnectionString("test.mdb"),
                    DevExpress.Xpo.DB.AutoCreateOption.DatabaseAndSchema
                    );
            }
            void GenerateTestData(int recordCount) {
                using (UnitOfWork s = new UnitOfWork()) {
                    if (s.FindObject<TestObject>(null) != null) return;
                    for (int i = 0; i < recordCount; i++) {
                        TestObject o = new TestObject(s) { HasAttachment = false, Sent = DateTime.Now, Subject = string.Format("Subject {0}", i) };
                    }
                    s.CommitChanges();
                }
            }
        }
    }
    

The following examples demonstrate how to use these data sources to bind data to a WPF data grid: