Results 1 to 7 of 7

Thread: datagridview looping problem

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2012
    Location
    Colchester, England, U.K.
    Posts
    19

    Question datagridview looping problem

    I know this must have a simple answer but I can't see it for the life of me.
    Why does the following code NOT loop through each row of the datagrid.
    The underlying database has 19 entries as does the datagridview BUT it loops 19 times on the first record!?

    Code:
            Me.MembersTableAdapter.FillBy(Me.DsGtacs.Members, gtID)   ' GTID = 2 digit string
            For Each row As DataGridViewRow In rsMembers.Rows
                '            If Not row.IsNewRow Then
                CheckAge(rsMembers.CurrentRow.Cells(10).Value)   ' Checkage is a Function in a module
                If AgeNow < 0 Then AgeNow = 0
                rsMembers.CurrentRow.Cells(31).Value = AgeNow
                '            End If
            Next
    I've commented out the IsNewRow but it did exactly the same with it uncommented.
    If someone could point out what I'm missing I would be most grateful.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: datagridview looping problem

    It's because you use CurrentRow inside the loop, which is the currently selected row every iteration, instead of 'row' which is your loop range variable. It's like you picking up and egg from an egg carton and saying "for each egg in this carton, I'm going to crack the egg in my hand". There are 12 eggs in the carton but only one egg in your hand, so you're going to crack that one egg one time each for the 12 eggs in the carton.

    Regardless, why are you loping through the rows of the grid at all? You have a typed DataSet so you obviously have a DataTable and that DataTable should be bound to the grid via a BindingSource. It's the BindingSource that ou should be looping through. That's what it's there for. The grid is there for the user to work with visually, you should pretty much never need to touch the grid in code.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2012
    Location
    Colchester, England, U.K.
    Posts
    19

    Re: datagridview looping problem

    Thanks for your remarkably quick response.
    I'm very new to vb2010, currently trying to convert a VB6 app into vb2010.
    I don't have (think I have) a datatable and I am struggling to find a way to iterate through a dataset changing one column with new information and then saving back to the database. Using the DataGridView seemed like a good idea.
    I've a table adapter in which I have created a query which I get at through the FillBy and I have a binding source.
    I imagined that when the next statement was read the cursor would move to the next row in the datagridview which then became the currentrow.
    I'm afraid that I'm finding the differences between the old recordsets in VB6 and the new datasets etc in vb2010 rather confusing. I started out with dBase3 and have progressed from there and I dearly would like to get to grips with vb.Net but I'm now old hence what will appear to be very stupid questions.
    Sorry.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: datagridview looping problem

    You do have a DataTable. This is it here:
    Code:
    Me.MembersTableAdapter.FillBy(Me.DsGtacs.Members, gtID)
    Using the grid is a very bad idea. Like I said, a DataGridView is a UI element to allow the user to work with the data. Even if you have a grid you should not use it in code. If the DataTable is bound to a grid then you work with the BindingSource. If you don't need the grid to display the data to the user then you just work directly with the DataTable in code. Here's an example of setting a particular column in each row of a typed DataTable:
    vb.net Code:
    1. For Each row In myDataTable
    2.     row.SomeColumn = someValue
    3. Next
    You would then call Update on your TableAdapter and pass the DataTable as an argument to save the changes, much as you called Fill to retrieve the data in the first place.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jun 2012
    Location
    Colchester, England, U.K.
    Posts
    19

    Re: datagridview looping problem

    Many thanks, I'll have a go and let you know how I get on.
    Warm regards,
    Tim

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jun 2012
    Location
    Colchester, England, U.K.
    Posts
    19

    Re: datagridview looping problem

    Hi again,
    I tried entering the following as per last post
    Code:
    For Each row In Me.DsGtacs.Members
    but I get this error
    Error 'row' is not declared. It may be inaccessible due to its protection level.
    so couldn't go any further.

    Did a little more searching online and found another response from you about the datarowview so tried this and it worked.
    Code:
    Me.MembersTableAdapter.FillBy(Me.DsGtacs.Members, gtID)
    For Each view As DataRowView In Me.DsGtacs.Members.DefaultView
        CheckAge(view.Item(10))
        If AgeNow < 0 Then AgeNow = 0
        view.Item(31) = AgeNow
    Next
    Me.Validate()
    Me.MembersTableAdapter.Update(DsGtacs.Members)
    So many thanks although I'd still like to know the reason that row threw up an error, if you've time.
    Last edited by timjohn; Jul 24th, 2012 at 03:52 AM.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jun 2012
    Location
    Colchester, England, U.K.
    Posts
    19

    Re: datagridview looping problem

    Just a quick question.
    Having got the datarowview loop working can I add a new record to the datarowview. I've looked on msdn but there doesn't appar to be an add new method.
    So wondering how I add a new record.
    Thanks.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width