Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why is removing rows in DataGridView so slow?

11 views
Skip to first unread message

Pete Moss

unread,
Aug 2, 2007, 10:02:07 AM8/2/07
to
I have a DataGridView with VirtualMode = true. The performance is great for
displaying many thousands of rows. However, if I remove a block of rows in
the middle of the grid, it takes multiple seconds while the scroll bar moves
around a lot, then the rows below finally get repainted. If I add the same
block of rows back in, performance is fine. The added rows appear almost
immediately.

The problem seems to be worse as I move farther down in the row set.

I tried 2 approaches to removing these rows. First, I simple decrement the
RowCount by the number of rows I want to remove. The 2nd approach was to do
this:

for (int n = nStart; n < nStart + count; n++)
grid.Rows.RemoveAt(n);

Same performance. In fact, when I use Reflector to see what RemoveAt() is
doing, it looks like it just decrements the RowCount.

Why should it be so slow? Is there anything I can do to speed this up?

Pete Moss

unread,
Aug 4, 2007, 2:54:01 PM8/4/07
to
In doing some more on line trolling, I found a solution on a blog post. The
answer is clear all the Rows, then set the RowCount to what I want. Here is
what I can do to remove a bunch of records from deleteStart to deleteStart +
count:

// Cache the first visible cell because when we clear the rows, we would
lose our scroll position
DataGridViewCell cell = grid.FirstDisplayedCell;
int firstVisibleColIndex = cell.ColumnIndex;
int firstVisibleRowIndex = cell.RowIndex;

// New RowCount after I remove the block of Rows
int rowCount = grid.RowCount - count;

grid.Rows.Clear();
grid.RowCount = rowCount;

cell = grid[firstVisibleColIndex, firstVisibleRowIndex];
grid.FirstDisplayedCell = cell;

Works great! And it happens essentially instantaneously. Note that the above
code is display only. I didn't show you the BL that describes how I remove
the rows from my underlying datastore, but that code executes in almost no
time anyway so that was not my issue.

kristywinters

unread,
Nov 12, 2009, 12:11:11 PM11/12/09
to
I had this issue as well, but found an easier way to resolve it. Just switch to RemoveAt, its instant and much cleaner.

Old code:

foreach (DataGridViewRow dgvRow in dgvDevices.SelectedRows)
dgvDevices.Rows.Remove(dgvRow);

New code:

foreach (DataGridViewRow dgvRow in dgvDevices.SelectedRows)
dgvDevices.Rows.RemoveAt(dgvRow.Index);


The remove used to take forever, but now its instant.

PeteMos wrote:

In doing some more on line trolling, I found a solution on a blog post.

04-Aug-07

grid.Rows.Clear();
grid.RowCount = rowCount;

Previous Posts In This Thread:

On Thursday, August 02, 2007 10:02 AM
PeteMos wrote:

Why is removing rows in DataGridView so slow?

On Saturday, August 04, 2007 2:54 PM
PeteMos wrote:

grid.Rows.Clear();
grid.RowCount = rowCount;

EggHeadCafe - Software Developer Portal of Choice
Pass Classes in ASP.NET with LosFormatter
http://www.eggheadcafe.com/tutorials/aspnet/25030f00-fd90-4fd8-9d03-4135280f8c3a/pass-classes-in-aspnet-w.aspx

0 new messages