Every once in a while, someone asks me "What’s the best way to bind 1,000,000+ records to RadGridView?” The first thought that pops into my head is, “Why in the world would you want to do that?” But – as there is always a reason for everything, good or bad, I typically go ahead and answer this question by suggesting the use of Virtual Mode. Virtual Mode allows you to implement your own data management operations for RadGridView. This means, instead of setting radGridView1.DataSource = bagillions of objects, you can intelligently manage how objects are loaded and cached behind the scenes. In doing so, you can save memory and increase the overall performance of your application.
Setting up the RadGridView to work in Virtual Mode is actually quite simple. There are three properties and two events that you should primarily be concerned with.
Using the properties and events I’ve described above, an extremely simple implementation of Virtual Mode is as follows.
public partial class MainForm : RadForm{ public MainForm() { InitializeComponent(); } // storage for data to be displayed in RadGridView private string[,] _dataCache; private void MainForm_Load(object sender, EventArgs e) { // Create some generic data // 3 columns, 5 rows _dataCache = new string[,] { {"1", "5", "1"}, {"2", "4", "2"}, {"3", "3", "3"}, {"4", "2", "4"}, {"5", "1", "5"} }; // setup RadGridView for Virtual Mode radGridView1.VirtualMode = true; radGridView1.EnableGrouping = false; radGridView1.EnableFiltering = false; radGridView1.EnableSorting = false; radGridView1.ColumnCount = 3; // 3 columns in _dataCache radGridView1.RowCount = 5; // 5 rows in _dataCache radGridView1.CellValueNeeded += new GridViewCellValueEventHandler(radGridView1_CellValueNeeded); } void radGridView1_CellValueNeeded(object sender, GridViewCellValueEventArgs e) { // retrieve the request value from the cache e.Value = _dataCache[e.RowIndex, e.ColumnIndex]; }}
As you can imagine, there are an endless number of possibilities for managing data behind the scenes when using Virtual Mode. How you set it up depends on if you are looking to improve performance, decrease memory usage, or a mix of both. Before changing the VirtualMode flag to true, you should also consider if displaying millions of records in the RadGridView is really the best route to take with your application.
If you are interested in reading more about implementing Virtual Mode, I recommend taking a look at the documentation.