Datagridview delete problem
-
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
-
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
-
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] }
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
-
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
Do it in reverse dataGridView1.RowCount: i=0; i--
Never underestimate the power of human stupidity RAH
-
Do it in reverse dataGridView1.RowCount: i=0; i--
Never underestimate the power of human stupidity RAH
-
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
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
-
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