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 delete problem

Datagridview delete problem

Scheduled Pinned Locked Moved C#
helpquestion
7 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.
  • H Offline
    H Offline
    half life
    wrote on last edited by
    #1

    hi, here's the layout: - datagridview with 5 columns, last column is CheckBox Column. the purpose : - by pressing a button = deleting each row that has a marked CheckBox the current algorithem : if (dataGridView1.RowCount > 0) { for (int k = 0; k < dataGridView1.RowCount; k++) { if ((bool)dataGridView1.Rows[k].Cells[5].Value) { dataGridView1.Rows.RemoveAt(k); } } } the problem : after the first deletion, the "(bool)dataGridView1.Rows[k].Cells[5].Value" throws this excption : Object reference not set to an instance of an object. anybody ??? THANKS :) :)

    Have Fun Never forget it

    L 2 Replies Last reply
    0
    • H half life

      hi, here's the layout: - datagridview with 5 columns, last column is CheckBox Column. the purpose : - by pressing a button = deleting each row that has a marked CheckBox the current algorithem : if (dataGridView1.RowCount > 0) { for (int k = 0; k < dataGridView1.RowCount; k++) { if ((bool)dataGridView1.Rows[k].Cells[5].Value) { dataGridView1.Rows.RemoveAt(k); } } } the problem : after the first deletion, the "(bool)dataGridView1.Rows[k].Cells[5].Value" throws this excption : Object reference not set to an instance of an object. anybody ??? THANKS :) :)

      Have Fun Never forget it

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      The problem is: You count the rows. and pass as k, but the .rows[k] is zero based index, so you need to "Count -1" Another method is: foreach(DataGridViewRowsCollection dvrc in dataGridView1.Rows) { // To show how to contiune // dvrc.cell[5] }

      H 1 Reply Last reply
      0
      • L Lost User

        The problem is: You count the rows. and pass as k, but the .rows[k] is zero based index, so you need to "Count -1" Another method is: foreach(DataGridViewRowsCollection dvrc in dataGridView1.Rows) { // To show how to contiune // dvrc.cell[5] }

        H Offline
        H Offline
        half life
        wrote on last edited by
        #3

        1. for (int k = 0; k < dataGridView1.RowCount; k++) is'nt tha problem becase : k < dataGridView1.RowCount if i have 7 rows it'll start at 0 and at 6 becase 7<7 is false 2. i've tried to change it to foreach, but still the same problem 3. thanks :) :)

        Have Fun Never forget it

        M 1 Reply Last reply
        0
        • H half life

          1. for (int k = 0; k < dataGridView1.RowCount; k++) is'nt tha problem becase : k < dataGridView1.RowCount if i have 7 rows it'll start at 0 and at 6 becase 7<7 is false 2. i've tried to change it to foreach, but still the same problem 3. thanks :) :)

          Have Fun Never forget it

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          Do it in reverse dataGridView1.RowCount: i=0; i--

          Never underestimate the power of human stupidity RAH

          H 1 Reply Last reply
          0
          • M Mycroft Holmes

            Do it in reverse dataGridView1.RowCount: i=0; i--

            Never underestimate the power of human stupidity RAH

            H Offline
            H Offline
            half life
            wrote on last edited by
            #5

            Sorry, i've tried it already and it did'nt work :( :( :( but thanks anyway :) :) :)

            Have Fun Never forget it

            1 Reply Last reply
            0
            • H half life

              hi, here's the layout: - datagridview with 5 columns, last column is CheckBox Column. the purpose : - by pressing a button = deleting each row that has a marked CheckBox the current algorithem : if (dataGridView1.RowCount > 0) { for (int k = 0; k < dataGridView1.RowCount; k++) { if ((bool)dataGridView1.Rows[k].Cells[5].Value) { dataGridView1.Rows.RemoveAt(k); } } } the problem : after the first deletion, the "(bool)dataGridView1.Rows[k].Cells[5].Value" throws this excption : Object reference not set to an instance of an object. anybody ??? THANKS :) :)

              Have Fun Never forget it

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              I got what the problem is: example: [0] Item 1 [1] Item 2 [2] Item 3 If [1] and [2] are checked: After first delition: [0] Item 1 [] Deleted Item2 [1] Item 3 In other words, when you delete items you have less items in Datagrid view. make another count within For and make sure it count++ when you delete items. and you do like [k-count]. at first count is 0, and more items you delete, higher count woud get and correct problem

              H 1 Reply Last reply
              0
              • L Lost User

                I got what the problem is: example: [0] Item 1 [1] Item 2 [2] Item 3 If [1] and [2] are checked: After first delition: [0] Item 1 [] Deleted Item2 [1] Item 3 In other words, when you delete items you have less items in Datagrid view. make another count within For and make sure it count++ when you delete items. and you do like [k-count]. at first count is 0, and more items you delete, higher count woud get and correct problem

                H Offline
                H Offline
                half life
                wrote on last edited by
                #7

                first of all, thanks for responding, i've did managed to figure it out thanks anyway and have a cool week :) :)

                Have Fun Never forget it

                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