The Virtual Mode property of the DataGridView allows us to work with huge amounts of data

The Virtual Mode property of the DataGridView allows us to work with huge amounts of data. By setting this property, and some custom code, the DataGridView is loading it's data just in time and that's exactly what we want.

First you need to set the virtual mode property to true. Next thing is to tell the DataGridView how much records there are in total. And the third and last thing is to create the CellValueNeeded event which binds the data for the specific cell.

Like this:

            GetData();
            CreateCollums();
            TheDataGridView.CellValueNeeded += new DataGridViewCellValueEventHandler(dataGridView1_CellValueNeeded);
            TheDataGridView.RowCount = store.Count;
            TheDataGridView.VirtualMode = true;

I did a test with the Entity Framework 4 and stored 40.000 records in a generic list:
First we create a variable to hold the data:

        private List store;

Now get the data.

        private void GetData()
        {
            store = new TestEntities().testtable.ToList();
        }

Creating the collums by using reflection on the object created by the Entity Framework

        private void CreateCollums()
        {
            foreach (var item in new testtable().GetType().GetProperties() )
            {
                dataGridView1.Columns.Add(item.Name, item.Name);
            }
        }

The last thing is to bind the data. The CellValueNeededEvent is firing for every cell in the grid. In the eventargs you will find the columnsindex property and the rowindex property. I used some reflection to get the data from the entity object by rowindex ( instead of by the name ).

        void dataGridView1_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
        {
             
            e.Value = store[e.RowIndex].GetType().GetProperties()[e.ColumnIndex].GetValue(store[e.RowIndex], null); ;
        }

{{opmerking.Naam}}:

{{opmerking.OpmerkingText}}

            

Saving your comment....

Naam is verplicht!
Email is verplicht!
Opmerking is verplicht!