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. DataGridView and Deleting Rows

DataGridView and Deleting Rows

Scheduled Pinned Locked Moved C#
databasehelptutorialquestion
3 Posts 3 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.
  • U Offline
    U Offline
    User 3678645
    wrote on last edited by
    #1

    Hi, I have a DataGridView control on a POS application I am writing as a project. I am having issues when trying to remove certain items from the control. Below is an example :- 1 Item 1 $5.00 -Extra 1 -Extra 2 1 Item 2 $10.00 1 Item 3 $2.00 -Extra 1 I want the user to be able to click on an item (or extra) in the datagridview and delete it. I can do this for single items and extras and it works perfectly (using removeat)but when I select an item, I want to check if there are any "extras" associated with the item and remove them aswell. Extras are always immediately below the item in question. My stumbling point seems to be when I remove 1 using RemoveAt, the index numbers change and I get exceptions. This datagridview is not bound to any datasource and I am creating it on the fly. Any help with the logic would be muchly appreciated. Thanks, Daniel.

    H L 2 Replies Last reply
    0
    • U User 3678645

      Hi, I have a DataGridView control on a POS application I am writing as a project. I am having issues when trying to remove certain items from the control. Below is an example :- 1 Item 1 $5.00 -Extra 1 -Extra 2 1 Item 2 $10.00 1 Item 3 $2.00 -Extra 1 I want the user to be able to click on an item (or extra) in the datagridview and delete it. I can do this for single items and extras and it works perfectly (using removeat)but when I select an item, I want to check if there are any "extras" associated with the item and remove them aswell. Extras are always immediately below the item in question. My stumbling point seems to be when I remove 1 using RemoveAt, the index numbers change and I get exceptions. This datagridview is not bound to any datasource and I am creating it on the fly. Any help with the logic would be muchly appreciated. Thanks, Daniel.

      H Offline
      H Offline
      Heinzzy
      wrote on last edited by
      #2

      /// My stumbling point seems to be when I remove 1 using RemoveAt, the index numbers change and I get exceptions. delete items upward from bottom to top of datagrid

      1 Reply Last reply
      0
      • U User 3678645

        Hi, I have a DataGridView control on a POS application I am writing as a project. I am having issues when trying to remove certain items from the control. Below is an example :- 1 Item 1 $5.00 -Extra 1 -Extra 2 1 Item 2 $10.00 1 Item 3 $2.00 -Extra 1 I want the user to be able to click on an item (or extra) in the datagridview and delete it. I can do this for single items and extras and it works perfectly (using removeat)but when I select an item, I want to check if there are any "extras" associated with the item and remove them aswell. Extras are always immediately below the item in question. My stumbling point seems to be when I remove 1 using RemoveAt, the index numbers change and I get exceptions. This datagridview is not bound to any datasource and I am creating it on the fly. Any help with the logic would be muchly appreciated. Thanks, Daniel.

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        When you delete an item in a list, any list, the indexes of the remaining items may change. There are two easy ways to cope with that: 1. not moving at all: assuming a range of items may have to be deleted, if the first item the be deleted is at index x, use RemoveAt(x); and continue testing for more removals at the same index x, as each removal there will decrement the index of all the # subsequent items by one. 2. working backwards: first determine the range to be deleted, without actually deleting anything; then delete from high index to low index; this also works when the items of interest aren't consecutive. Beware, whatever you choose to do, a foreach will fail as an enumerator becomes invalid (and will throw an exception) when the collection gets modified. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        If you want my opinion or comment, ask in a forum or on my profile page; I will not participate in frackin' Q&A


        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