We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Deleting the LAST row from a databoundgrid...

Hi all. I am having a problem deleting the last row in a GridDataBoundGrid. Specifically: When I attempt to delete the last row in the grid through code, the row appears to delete. But, when I then click another row in the grid, the application appears to "freeze" for a moment, then an exception occurs (1st couple of lines of stack trace): System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index at System.Collections.ArrayList.get_Item(Int32 index) at System.Windows.Forms.CurrencyManager.EndCurrentEdit() : : : BUT, if I delete any other row except for the last row, everything runs fine. I don't understand this at all. I've included a ver small project (Visual Studio .NET 2003) that demonstrates this. Any ideas on what I'm doing wrong would be greatly appreciated!! Thanks! -Chris

1 Reply

Administrator May 29, 2003 05:32 PM UTC

The problem is because the current binder position is being left on the last row, and the last row is being removed (making the current position invalid). Until we can get this fixed, you can avoid the problem with a simple check to correct the position if you are deleting the last row.
int recNumber = this.techGrid.Binder.RowIndexToPosition(curRowIndex);
if(curRowIndex == this.techGrid.Model.RowCount && recNumber >0)
	this.techGrid.Binder.CurrentPosition = recNumber - 1;
this.techGrid.Binder.RemoveRecords(recNumber, recNumber);
Loader.
Live Chat Icon For mobile
Up arrow icon