That solution is still not working for me. I have solved it this way.
By iterating the grid in reverse order.
I first clear the cells, I am iterating through a grid to move selected rows to a second grid and clearing the rows in that foreach loop using:
this.gridControl1.ClearCells(GridRangeInfo.Row(x), true);
when that loop is done, I do another for loop starting at the largest row value to the first row
In this loop I look for a blank entry, like this:
for (int i = this.gridControl1.RowCount; i > 0; i--)
{
if (gridControl1[i, 2].Text.Length == 0)
{
this.gridControl1.Rows.RemoveRange(i, i);
}
}
You cant delete the rows in a for each loop because you get a collections modified error
In the for next loop, when you delete a row the number of rows (RowCount) is reduced by 1
If you did the loop from row 1 to the bottom, deleting row 1 causes row 2 to become row 1
if row 1 now has a length of 0 in the cell your loop skips over it.
For large grids, you could use a list and save the row numbers when clearing cells in the foreach loop
to save time when actually deleting them again going from highest to lowest row
Here is my foreach loop: It copies grid 1 selections to grid 2, erases cell data without deleting the rows
int r = NextAvailableRow(); // A method to search for an empty row checks cell length.
int x = 0;
foreach (GridRangeInfo range in this.gridControl1.Selections.Ranges)
{
Int32.TryParse(range.Info.ToString().Substring(1), out x);
this.gridControl2[r, 2].Text = this.gridControl1[x, 1].Text;
this.gridControl1.ClearCells(GridRangeInfo.Row(x), true);
}