-
Jul 17th, 2014,03:49 PM #1
Thread Starter
Fond Member
[RESOLVED] VB.net DataGridView For Each loop doesn't work on column after value inserted
I'm getting a strange result after updating a value in a DataGridView populated from a DataTable. The user correct clicks a menu and inserts a value into DataGridView1. Essentially marker it for removal:
Code:
Dim DNICol As String = Me.DNICombo.Text DataGridView1.SelectedRows(0).Cells.Item(DNICol).Value = "REMOVE"
This works corking. And then I want to loop through the DataGridView and remove the 'marked' row: Code:
For Each row As DataGridViewRow In DataGridView1.Rows DNI = row.Cells.Item(DNICol).Value If DNI = "REMOVE" Then DataGridView1.Rows.Remove(row) End If Next
The strange thing is if the DataTable that populates the DataGridView already has 'REMOVE' as a string in this column the code works neat. If the user updates the DataGridView via my right-click and can run across the value gets updated for that row only, simply when I run the For Each loop information technology finds 'REMOVE' on every row and deletes them all from the DataGridView.
-
Jul 17th, 2014,06:44 PM #2
Re: VB.net DataGridView For Each loop doesn't work on column afterwards value inserted
That code is just plain wrong no matter what. Y'all cannot enumerate a drove with a For Each loop and and so remove items from that same collection within the loop. I would have expected different symptoms only, regardless, that is never OK. If you lot desire to remove items from a collection then use a For loop, not a For Each loop, and loop from the end to the beginning, not the beginning to the finish. That fashion, removing an item will not affect the indexes of the items yous are yet to visit. Make that change and encounter where you're at.
-
Jul 17th, 2014,06:47 PM #three
Hyperactive Member
Re: VB.net DataGridView For Each loop doesn't work on column afterward value inserted
jmchilhinney, dont y'all think that Lawmaking:
DataGridView1.SelectedRows(0).Cells.Item(DNICol).Value = "REMOVE"
It should not exist similar this ?
-
Jul 17th, 2014,07:14 PM #4
Re: VB.net DataGridView For Each loop doesn't piece of work on column subsequently value inserted
Originally Posted by hamza.saleem
jmchilhinney, dont you recollect that
Code:
DataGridView1.SelectedRows(0).Cells.Item(DNICol).Value = "REMOVE"
It should not be like this ?
It'southward not necessarily how I would design the whole thing only, in context, there's nothing specifically wrong with that line of code. It's going to prepare the Value of a particular cell in the first selected row to "REMOVE". If that's what y'all desire to do then that code is going to do it.
-
Jul 17th, 2014,07:33 PM #5
Hyperactive Member
Re: VB.net DataGridView For Each loop doesn't work on column after value inserted
Originally Posted past jmcilhinney
It's not necessarily how I would blueprint the whole thing but, in context, in that location'south zero specifically wrong with that line of code. It's going to ready the Value of a item cell in the beginning selected row to "REMOVE". If that's what you lot want to do then that code is going to practice it.
Hmm...
-
Jul 18th, 2014,09:29 AM #6
Thread Starter
Addicted Member
Re: VB.net DataGridView For Each loop doesn't work on cavalcade after value inserted
hamza, I wouldn't focus on that line, it'south merely for proof of concept. I have to go research the differneces between a For Each and For Loop. I now understand the enumerating and deleting in the same loop won't work. I approximate I should pace through the whole datagridview, place which rows are marked for removal and so remove them in a split loop?
I'm a lilliputian lost.
-
Jul 18th, 2014,10:35 AM #7
Re: VB.net DataGridView For Each loop doesn't work on column after value inserted
Originally Posted past Fedaykin
hamza, I wouldn't focus on that line, it's but for proof of concept.
I accept to become research the differneces between a For Each and For Loop. I now understand the enumerating and deleting in the aforementioned loop won't work. I guess I should step through the whole datagridview, identify which rows are marked for removal and then remove them in a separate loop?
I'grand a little lost.
It'due south pretty simple. A For loop ordinarily goes from zero to the last index but you should become the other way. Use the loop counter equally an alphabetize to become a row and the delete the row if appropriate.
-
Jul 18th, 2014,11:22 AM #8
Thread Starter
Fond Fellow member
Re: VB.cyberspace DataGridView For Each loop doesn't work on column afterwards value inserted
Okay, here is what i came upwards with, but it isn't working nevertheless: Code:
For i Equally Integer = DataGridView1.Rows.Count - i To 0 If DataGridView1.Rows(i).Cells(DNICol).ToString = "DNI" Then DataGridView1.Rows.RemoveAt(i) End If Next
-
Jul 18th, 2014,11:34 AM #9
Thread Starter
Addicted Member
Re: VB.net DataGridView For Each loop doesn't work on column after value inserted
I fix a break indicate and it reads the "For i Equally Integer line....", but and then it just skips right by the "If DataGridView1.Rows(i)..." line. Code:
For i Equally Integer = DataGridView1.Rows.Count - 1 To 0 If DataGridView1.Rows(i).Cells(DNICol).Value = "DNI" Then DataGridView1.Rows.RemoveAt(i) Terminate If Side by side
-
Jul 18th, 2014,11:46 AM #10
Re: VB.net DataGridView For Each loop doesn't work on column after value inserted
You demand to specify `Footstep -1` if you want to loop backwards.
-
Jul 18th, 2014,12:04 PM #eleven
Thread Starter
Addicted Fellow member
Re: VB.net DataGridView For Each loop doesn't piece of work on column later on value inserted
Okay this works! Give thanks you for the lesson: Lawmaking:
For i Every bit Integer = DataGridView1.Rows.Count - 1 To 0 Footstep -1 If Non IsDBNull(DataGridView1.Rows(i).Cells(DNICol).Value) Then If DataGridView1.Rows(i).Cells(DNICol).Value = "DNI" Then DataGridView1.Rows.RemoveAt(i) End If End If Next
Ahh.. that feels better. Only now for my rant. Why on world practice I need to specify Step -one when I already reversed the higher number to 0? Logically the pace is ane or -one depending on if you ready your TO ascending or descending. The step *should* only need to be called out if information technology is greater than 1 or less than -1.
If they are going to brand you lot call out the -1 then logically information technology should exist written "0 To 5 Step -1". Or at to the lowest degree that would follow the same logic equally 0 To 5 with no stride specified (implied equally Footstep 1).
I love .Internet, only there is some stuff in here that you tin tell they just threw in as an afterward thought.
Last edited by Fedaykin; Jul 18th, 2014 at 12:07 PM.
-
Jul 18th, 2014,12:21 PM #12
Re: VB.net DataGridView For Each loop doesn't piece of work on cavalcade after value inserted
Originally Posted by Fedaykin
But now for my rant. Why on earth do I demand to specify Pace -i when I already reversed the college number to 0? Logically the step is one or -one depending on if you prepare your TO ascending or descending. The step *should* only need to be called out if it is greater than one or less than -ane.
If they are going to make you lot call out the -1 then logically it should exist written "0 To 5 Pace -i". Or at to the lowest degree that would follow the same logic as 0 To v with no step specified (implied as Step 1).
I love .Cyberspace, but there is some stuff in here that y'all can tell they just threw in as an subsequently thought.
Considering the Step value can exist anything, non just 1 or -1. Y'all can step past two, 5 -x or whatsoever. The rule is that the Stride defaults to ane if it's not specified explicitly. A case could be made for bold -1 if the start value is greater than the end value but that's not how information technology works. That'due south reasonable enough too. What if yous used variables for both the start and the end and you didn't ever desire to step backwards?
-
Jul 18th, 2014,04:11 PM #13
Thread Starter
Addicted Fellow member
Re: VB.cyberspace DataGridView For Each loop doesn't piece of work on column later on value inserted
Originally Posted by jmcilhinney
Because the Step value can be anything, not just 1 or -1. Yous tin can stride by 2, v -10 or whatever. The dominion is that the Step defaults to 1 if it'southward not specified explicitly. A case could be made for assuming -1 if the get-go value is greater than the end value but that's not how it works. That's reasonable enough besides. What if you used variables for both the outset and the end and you lot didn't e'er want to step backwards?
Ah, I didn't think of that. Still, it felt good to bluster.
0 Response to "How to Read All Rows From a Datagridview in Vb.net"
Post a Comment