A newer version of this page is available. Switch to the current version.

XPDataView Concepts

  • 4 minutes to read

The XPDataView is represented by the XPDataView class. It is intended only to display data from result sets, which can be obtained from a data store using direct SQL queries and stored procedures. The main difference from the XPCollection, XPCursor, and XPView, is that the XPDataView is session-less - no session is required to retrieve data from a data store, as it is already been retrieved into a result set.

An XPDataView provides built-in filtering and sorting capabilities to shape the resulting data on the client, as required. In addition, an XPDataView implements the IBindingList and ITypedList interfaces, so it can serve as a data source for visual data-aware controls (for instance, the XtraGrid).

Creating an XPDataView

At design time, you can create an XPDataView by dragging the corresponding item from the Data tab of the Toolbox onto your form or component. Then, you can manually populate the data view with columns via the XPDataView.Properties collection's built-in editor, allowing you to add/remove columns and specify their names and types. Result set contents can then be loaded into the data view in code, using the XPDataView.LoadData or XPDataView.LoadOrderedData method, as shown below.

using DevExpress.Xpo;
using DevExpress.Xpo.DB;

// ...
// For brevity, we omitted the code initializing session1 (a session 
// that is used to retrieve data from a data store into a result set).
SelectedData resultSet = session1.ExecuteQuery("SELECT Name, Age FROM [Person]");

// We assume that the Name and Age columns are created and stored in this
// exact order within the xpDataView1.Properties collection. So, there is
// no need to call LoadOrderedData and use column mapping information.

At runtime, you can create an XPDataView using overloaded constructors, which, in addition to creating the data view, allow you to populate it with columns and load result set data into it, in one step. Alternatively, you can create an XPDataView with certain settings, and later, populate it with required columns (via XPDataView.AddProperty, DataViewPropertiesCollection.Add, XPDataView.PopulateProperties and XPDataView.PopulatePropertiesOrdered) and load the result set data (via XPDataView.LoadData or XPDataView.LoadOrderedData).

The following example demonstrates how to create a new XPDataView, populate its XPView.Properties collection with columns and load data.

using DevExpress.Xpo;
using DevExpress.Xpo.DB;

// ...
SelectedData resultSet = session1.ExecuteQuery("SELECT Name, Age FROM [Person]");
XPDataView xpDataView1 = new XPDataView(session1.Dictionary, session1.GetClassInfo<Person>(), 
    new string[] { "Name", "Age" });

The code examples above assume that XPDataView columns are stored in the XPDataView.Properties collection in the exact order as corresponding result set columns. If the order is different or a result set contains columns that you do not want to display, call the XPDataView.LoadOrderedData method and use column mapping information. To learn how to load result set data using mapping information, refer to this method's description.

Filtering an XPDataView

The XPDataView allows its records to be filtered on the client. To apply filter criteria use the XPDataView.Filter or XPDataView.FilterString property. For more information on filtering, refer to topics in the Filtering section.

In the following example, the data view contents are filtered to display people between the ages of 30 and 50 (inclusive).

xpDataView1.FilterString = "Age Between (30, 50)";

Sorting Data in an XPDataView

You can sort XPDataView contents by column values on the client. To accomplish this, use the XPDataView.Sorting property. For more information on sorting, refer to topics in the Sorting section.

The code sample below shows how to sort XPDataView by descending age and then, by the ascending order of names.

SortingCollection sortCollection = new SortingCollection();
sortCollection.Add(new SortProperty("Age", SortingDirection.Descending));
sortCollection.Add(new SortProperty("Name", SortingDirection.Ascending));
xpDataView1.Sorting = sortCollection;