VirtualServerModeSource Class
A data source that features event-based data operations: async data load, sorting, filtering and infinite scrolling through records (in a bound Windows Forms GridControl).
Namespace: DevExpress.Data
Assembly: DevExpress.Data.v20.2.dll
NuGet Packages: DevExpress.Data, DevExpress.WindowsDesktop.Data
Declaration
[ToolboxBitmap(typeof(ResFinder), "Bitmaps256.VirtualServerModeSource.bmp")]
public class VirtualServerModeSource :
Component,
IListSource,
ISupportInitialize
Remarks
The VirtualServerModeSource is an event-based data source for a Windows Forms Data Grid (GridControl). The events the data source provides allow you to asynchronously load data in batches, while taking into account a bound grid control’s sort and filter configuration.
A Data Grid bound to a VirtualServerModeSource supports infinite scrolling. On startup, the Data Grid fetches an initial batch of records from the data source. The control does not know the total number of records in the data source and thus, its scroll bar always reflects the current number of loaded rows. Once an end-user scrolls to the last loaded record, the grid requests an additional batch of records and adjusts its scroll bar to match the new loaded record count.
Note
Data grouping is not supported in a Data Grid control when it is bound to a VirtualServerModeSource.
The following are two main events the VirtualServerModeSource provides:
- VirtualServerModeSource.ConfigurationChanged -
Fires on initial data load and reload in the bound grid control, and when the data grid’s sort and filter configuration changes. Allows you to initialize the data source and (optionally) return an initial batch of records.
- VirtualServerModeSource.MoreRows - Fires when a bound grid control asks the data source for a new batch of rows.
using DevExpress.Data;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraGrid;
using System.Collections.Generic;
using System.Windows.Forms;
namespace DXApplication57 {
public partial class Form1 : RibbonForm {
public Form1() {
InitializeComponent();
VirtualServerModeSource virtualServerModeSource = new VirtualServerModeSource();
virtualServerModeSource.RowType = typeof(Product);
virtualServerModeSource.ConfigurationChanged += virtualServerModeSource_ConfigurationChanged;
virtualServerModeSource.MoreRows += virtualServerModeSource_MoreRows;
GridControl gridControl = new GridControl();
gridControl.Parent = this;
gridControl.Dock = DockStyle.Fill;
gridControl.DataSource = virtualServerModeSource;
}
int batchSize = 20;
int maxRowCount = 500;
private void virtualServerModeSource_ConfigurationChanged(object sender, DevExpress.Data.VirtualServerModeRowsEventArgs e) {
e.UserData = GetRows(0);
}
private void virtualServerModeSource_MoreRows(object sender, DevExpress.Data.VirtualServerModeRowsEventArgs e) {
e.RowsTask = System.Threading.Tasks.Task.Factory.StartNew(() => {
bool moreRows = e.CurrentRowCount < maxRowCount - batchSize;
return new VirtualServerModeRowsTaskResult(GetRows(e.CurrentRowCount), moreRows, e.UserData);
}, e.CancellationToken);
}
List<Product> GetRows(int startRowIndex) {
List<Product> lst = new List<Product>();
for (int i = startRowIndex; i < startRowIndex + batchSize; i++)
lst.Add(new Product { ID = i, Name = $"Product{i}" });
return lst;
}
}
public class Product {
public int ID { get; set; }
public string Name { get; set; }
}
}
Additional events allow you to control data shaping operations invoked in the bound grid control and enable CRUD operations:
- VirtualServerModeSource.GetUniqueValues - Allows you to provide values for columns’ filter dropdowns.
- VirtualServerModeSource.TotalSummary - Fires when a grid control asks the data source to calculate total summaries.
- VirtualServerModeSource.CanPerformColumnServerAction - Allows you to specify whether the requested filtering, sorting or summary calculation operation needs to be accepted or canceled.
- VirtualServerModeSource.AcquireInnerList -
This event can be handled to provide an inner list that will be storage for rows fetched using the VirtualServerModeSource’s events. To enable CRUD operations in a bound Data Grid, you need to provide an inner list that supports these operations. If no inner list is supplied (or you do not handle the AcquireInnerList event), CRUD operations are disabled in the grid control.
At design time, to create columns in a bound grid control for fields the VirtualServerModeSource provides, specify the VirtualServerModeSource.RowType property.
If the RowType
property is not set, handle the AcquireInnerList event to manually supply a list that will store rows fetched by VirtualServerModeSource’s events.
Demo: Infinite Scrolling module in the XtraGrid MainDemo