Delete last row from a datagrid bound to an arraylist, without getting index out of range when picking another row afterwards.?
-
Hi! Im using an arraylist bound to a datagrid to show some info. I also let the user delete rows from the datagrid using a contextmenu. IE rightclick on the row, choose "delete item", and the row disappears. Everything works fine except when I delete the last row, then when I choose another row I get a "Index out of range exception". this is my deletemethod: private void DeleteItem(object sender, System.EventArgs e) { //get the index of the selected Item, same index in the ArrayList int index = commentGrid.CurrentCell.RowNumber; try { comments.Comments.RemoveAt(index); if (comments.Comments.Count < 1) { menuItemExport.Enabled = false; toolBar.Buttons[2].Enabled = false; } dataChanged = true; commentGrid.DataSource = null; commentGrid.Refresh(); commentGrid.DataSource = comments.Comments; commentGrid.Refresh(); } catch(ArgumentOutOfRangeException) { return; } } I heard this was some kind of bug in .NET, but I dont know how to work around the problem! Any help is much appreciated. thanks! /Rickard