5

I have a DataGridView bound to a list of custom objects created on the fly. This list is not very large, 5000 items maximum. However, the values of the custom objects change every 50ms and the grid hangs the application while refreshing the values and it ultimately crashes.

My question is: is there a way to "virtualize" the data binding of the DataGridView so that only the rows that are actually seen on the screen are refreshed?

EDIT: I found out why my DataGridView was so slow and it had nothing to do with data binding. So this question is no longer relevant. As a side note, I think the DataGridView already refreshes only the visible rows when a ListChanged event occurs.

Julien Poulin
  • 12,737
  • 10
  • 51
  • 76

5 Answers5

2

A good article on Virtual mode (DataGridView). - http://www.codeproject.com/KB/books/PresentDataDataGridView.aspx#7

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
1

Spent quite a while searching this issue and this question kept popping up, so I'll link here the answer which solved my problem: https://stackoverflow.com/a/9348149/674884

I have a DataGridView bound to a BindingSource which is also bound by ComboBoxes and TextBoxes used to edit the data. Every Leave event on the editors had a big lag which was caused by the DataGridView redrawing all it's rows when the data was updated. This happened even when using VirtualMode.

The problem was caused by the AllCells autosize setting of DataGridView columns. Every time a value changed the DataGridView went through all the rows to find the longest string for autosizing the column. After disabling autosizing I realized that even the databound DataGridView draws only the visible rows, so no need to use the VirtualMode.

Community
  • 1
  • 1
Mika Tähtinen
  • 963
  • 7
  • 10
0

I think you will want to look into using the DataGridView in virtual mode.

Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343
0

What is stopping you just pulling the visible sub-set of items instead of the full 5000?

philwilks
  • 669
  • 5
  • 16
0

I don't know if ListChanged already refreshes only the visible rows. I can't see any indention in MSDN. I will look into DataGridView implementation later and update.

Anyhow the follow works for me:

int scrollPositionFirst = dataGridView1.FirstDisplayedScrollingRowIndex;

// displayedRowCount -> is our visible rows count
var displayedRowCount = dataGridView1.DisplayedRowCount(false);

// the loop will iterate in the amount of the displayed rows
for (int rowCount = 0; index < displayedRowCount; rowCount++)
{
    // scrollPositionFirst is our first visable row 
    // so scrollPositionFirst is our starting point
    var set = (Set)dataGridView1.Rows[scrollPositionFirst].DataBoundItem;

    // we update the grid from the first visable(displayed) index 
    UpdateMainGrid(scrollPositionFirst, set);

    // increment index -> this is done because we want to update the next visible row
    scrollPositionFirst++;  
}
Sahelanthropus
  • 164
  • 1
  • 4
  • 13