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?
// 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.
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