How to Read Datagridview Row by Row in Vb.net

  1. #one

    Fedaykin is offline

    Thread Starter

    Addicted Member


    Resolved [RESOLVED] VB.net DataGridView For Each loop doesn't work on column after value inserted

    I'm getting a strange result later on updating a value in a DataGridView populated from a DataTable.

    The user right clicks a menu and inserts a value into DataGridView1. Essentially marking it for removal:

    Code:

    Dim DNICol As String = Me.DNICombo.Text DataGridView1.SelectedRows(0).Cells.Detail(DNICol).Value = "REMOVE"
    This works corking. 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)                 Finish If Next
    The foreign thing is if the DataTable that populates the DataGridView already has 'REMOVE' equally a string in this column the lawmaking works peachy. If the user updates the DataGridView via my right-click and can see the value gets updated for that row only, but when I run the For Each loop it finds 'REMOVE' on every row and deletes them all from the DataGridView.

  2. #ii

    Re: VB.net DataGridView For Each loop doesn't work on column after value inserted

    That code is just plain wrong no affair what. You cannot enumerate a collection with a For Each loop and then remove items from that same drove inside the loop. I would accept expected different symptoms just, regardless, that is never OK. If yous want to remove items from a collection then apply a For loop, not a For Each loop, and loop from the end to the start, not the showtime to the end. That style, removing an item will not affect the indexes of the items y'all are notwithstanding to visit. Make that change and come across where y'all're at.

  3. #three

    hamza.saleem is offline

    Hyperactive Member


    Re: VB.net DataGridView For Each loop doesn't work on column subsequently value inserted

    jmchilhinney, dont you recollect that

    Code:

    DataGridView1.SelectedRows(0).Cells.Item(DNICol).Value = "REMOVE"
    Information technology should not exist similar this ?

  4. #four

    Re: VB.net DataGridView For Each loop doesn't work on column after value inserted

    Quote Originally Posted past hamza.saleem View Post

    jmchilhinney, dont y'all think that

    Code:

    DataGridView1.SelectedRows(0).Cells.Item(DNICol).Value = "REMOVE"

    It should non be similar this ?

    It'southward non necessarily how I would design the whole thing merely, in context, there's zilch specifically wrong with that line of code. It's going to set the Value of a detail cell in the start selected row to "REMOVE". If that's what you want to practice and then that lawmaking is going to practise it.

  5. #v

    hamza.saleem is offline

    Hyperactive Fellow member


    Re: VB.internet DataGridView For Each loop doesn't piece of work on column after value inserted

    Quote Originally Posted by jmcilhinney View Post

    It's not necessarily how I would design the whole affair only, in context, there'south aught specifically wrong with that line of code. It's going to fix the Value of a particular cell in the first selected row to "REMOVE". If that'southward what you lot want to do then that code is going to do it.

    Hmm...

  6. #vi

    Fedaykin is offline

    Thread Starter

    Addicted Member


    Re: VB.net DataGridView For Each loop doesn't piece of work on column after value inserted

    hamza, I wouldn't focus on that line, it'due south but for proof of concept.

    I have to go enquiry the differneces between a For Each and For Loop. I at present understand the enumerating and deleting in the aforementioned loop won't work. I guess I should stride through the whole datagridview, identify which rows are marked for removal and then remove them in a carve up loop?

    I'm a little lost.


  7. #7

    Re: VB.cyberspace DataGridView For Each loop doesn't work on column afterward value inserted

    Quote Originally Posted by Fedaykin View Post

    hamza, I wouldn't focus on that line, it's but for proof of concept.

    I have to go inquiry the differneces betwixt a For Each and For Loop. I now sympathize the enumerating and deleting in the same loop won't piece of work. I gauge I should pace through the whole datagridview, identify which rows are marked for removal and then remove them in a separate loop?

    I'm a little lost.

    Information technology'south pretty simple. A For loop usually goes from zero to the last index but yous should go the other style. Use the loop counter as an index to become a row and the delete the row if appropriate.

  8. #8

    Fedaykin is offline

    Thread Starter

    Addicted Member


    Re: VB.net DataGridView For Each loop doesn't work on column subsequently value inserted

    Okay, hither is what i came up with, simply information technology isn't working nevertheless:

    Code:

                                      For i As Integer = DataGridView1.Rows.Count - 1 To 0             If DataGridView1.Rows(i).Cells(DNICol).ToString = "DNI" Then                 DataGridView1.Rows.RemoveAt(i)             End If         Next

  9. #9

    Fedaykin is offline

    Thread Starter

    Addicted Fellow member


    Re: VB.net DataGridView For Each loop doesn't work on column after value inserted

    I set a break indicate and information technology reads the "For i Equally Integer line....", but then information technology just skips right past the "If DataGridView1.Rows(i)..." line.

    Lawmaking:

                                      For i As 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

  10. #10

    Re: VB.net DataGridView For Each loop doesn't work on column after value inserted

    You lot need to specify `Step -1` if you want to loop backwards.

  11. #11

    Fedaykin is offline

    Thread Starter

    Fond Member


    Re: VB.net DataGridView For Each loop doesn't work on cavalcade after value inserted

    Okay this works! Cheers 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) And then                 If DataGridView1.Rows(i).Cells(DNICol).Value = "DNI" Then                     DataGridView1.Rows.RemoveAt(i)                 Cease If             End If         Side by side
    Ahh.. that feels improve.

    But now for my rant. Why on world do I need to specify Stride -1 when I already reversed the college number to 0? Logically the step is 1 or -1 depending on if you set your TO ascending or descending. The step *should* merely need to be chosen out if it is greater than 1 or less than -1.

    If they are going to make you call out the -ane and then logically it should be written "0 To 5 Step -i". Or at least that would follow the same logic as 0 To 5 with no stride specified (implied as Pace 1).

    I honey .NET, only at that place is some stuff in hither that you lot tin tell they just threw in as an later thought.

    Terminal edited by Fedaykin; Jul 18th, 2014 at 12:07 PM.

  12. #12

    Re: VB.internet DataGridView For Each loop doesn't work on column subsequently value inserted

    Quote Originally Posted past Fedaykin View Post

    Simply now for my bluster. Why on earth practice I need to specify Step -1 when I already reversed the higher number to 0? Logically the step is one or -1 depending on if you set up your TO ascending or descending. The step *should* only need to exist chosen out if information technology is greater than 1 or less than -1.

    If they are going to make y'all call out the -1 then logically it should be written "0 To v Step -one". Or at least that would follow the same logic as 0 To 5 with no step specified (unsaid as Step one).

    I dearest .Cyberspace, but there is some stuff in hither that you lot tin can tell they just threw in equally an after thought.

    Considering the Step value can be anything, non just 1 or -ane. You can footstep by ii, five -x or whatever. The dominion is that the Step defaults to 1 if it's non specified explicitly. A case could be made for assuming -one if the showtime value is greater than the cease value but that'southward non how it works. That's reasonable plenty too. What if y'all used variables for both the get-go and the finish and you didn't ever want to step backwards?

  13. #xiii

    Fedaykin is offline

    Thread Starter

    Addicted Member


    Re: VB.cyberspace DataGridView For Each loop doesn't piece of work on column after value inserted

    Quote Originally Posted by jmcilhinney View Post

    Considering the Pace value can exist anything, not just ane or -1. You can step past two, 5 -x or whatsoever. The rule is that the Footstep defaults to 1 if information technology's not specified explicitly. A case could be fabricated for assuming -1 if the beginning value is greater than the end value just that's non how information technology works. That'due south reasonable plenty too. What if you used variables for both the showtime and the end and you didn't ever want to step backwards?

    Ah, I didn't think of that. Still, information technology felt skilful to bluster.

Posting Permissions

  • You may non post new threads
  • You may non postal service replies
  • You may non post attachments
  • You may non edit your posts
  • BB lawmaking is On
  • Smilies are On
  • [IMG] lawmaking is On
  • [VIDEO] code is On
  • HTML code is Off

Click Here to Aggrandize Forum to Full Width

whitlamfortalwyneho.blogspot.com

Source: https://www.vbforums.com/showthread.php?771169-RESOLVED-VB-net-DataGridView-For-Each-loop-doesn-t-work-on-column-after-value-inserted

0 Response to "How to Read Datagridview Row by Row in Vb.net"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel