Telerik blogs

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.

Implementing Virtual Mode

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. 

  • Properties
    • public bool VirtualMode { get; set; }
      • Setting this property to true will enable the RadGridView for Virtual Mode. Note that if you enable virtual mode, you will lose some basic functionality included with the RadGridView. This includes things such as filtering, sorting, grouping, etc. If you want to use these features, you will have to implement their functionality manually.
    • public int RowCount { get; set; }
      • You will need to set this to the number of rows you plan on displaying at any one time. For example, if you plan to implement paging, you might set this to 20 to show 20 rows per page. The user would only be able to use the scrollbar to scroll through the 20 displayed items. In order to get to the next page, they would have to click a button or perform some sort of action elsewhere on the screen. If you want the user to be able to scroll through all items, you’ll need to set this value to the maximum number of items to be displayed.
    • public int ColumnCount { get; set; }
      • This value specifies how many columns you will be displaying. If you plan on displaying all properties or column values from your data source objects, you will want to set this value equal to the total number of properties. If you only plan on displaying a subset of properties or column values, you will need to set this value equal to that amount instead.
  • Events
    • public event GridViewCellValueEventHandler CellValueNeeded;
      • This event will be triggered for every cell of the RadGridView as it gets displayed to the user. The event arguments contain the RowIndex and ColumnIndex for which this event was triggered. Using these values, you can retrieve the correct value to be displayed and set it to the Value property of the event arguments.
    • public event GridViewCellValueEventHandler CellValuePushed;
      • This event will be triggered for a cell if the user changes the data it contains. Like the CellValueNeeded event, this event also receives event arguments that contain the RowIndex and ColumnIndex for which this event was triggered. Using these values, you can update the corresponding value in the data source. 

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];
    }
}
 

 

Conclusion

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.


Comments

Comments are disabled in preview mode.