Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Delete last row from a datagrid bound to an arraylist, without getting index out of range when picking another row afterwards.?

Delete last row from a datagrid bound to an arraylist, without getting index out of range when picking another row afterwards.?

Scheduled Pinned Locked Moved C#
helpcsharpdatabasetutorialquestion
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    spAAwn
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups