Results 1 to 9 of 9

Thread: [RESOLVED] Iterating though a DataTable and updating without "For Each"

  1. #1

    Thread Starter
    Fanatic Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,020

    Resolved [RESOLVED] Iterating though a DataTable and updating without "For Each"

    I have a strongly typed DataTable bound to a DataGridView. I created a loop "For Each drPlayerSelected As Main.PlayersRow In Main.Tables("Players").Rows". Worked well and I love being able to use the column names to reference the field values. But in this process there is something that takes a long time and I am moving it to a BackGroundWorker so I can update the DataGridView and not freeze up the application. As far as I can tell there's no way to have a BGW in a loop so I was going to make a loop using the events like the BGW complete to manually loop though them all handing off between procedures. So I thought I'd use an index like below but this doesn't work because it's not the generic row type.
    vb.net Code:
    1. Dim drPlayerSelected As Main.PlayersRow
    2. drPlayerSelected = Main.Tables("Players").Rows(0)
    So how do I create an object that points to a row by index so I can use the typed column names.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,242

    Re: Iterating though a DataTable and updating without "For Each"

    Rather than a BGW, you might consider using a Task, which could pass off from one to the next, or you could launch a different task for each item.

    However, if you are going to do a whole bunch, why not put the whole loop into the BGW? On the other hand, if the reason that each one takes a long time is because they spend a lot of time waiting for a response from somewhere, such as a web service, then you'd be far better off using Tasks. A loop in the BGW would mean that they'd be performed one after another, so you'd see the entire wait time before the loop completed, whereas with Tasks, they might all run simultaneously.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Fanatic Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,020

    Re: Iterating though a DataTable and updating without "For Each"

    I have not heard of tasks. I will research it immediately. Do you know of any good tutorials off hand? If not I'm sure I can find some via Google.

    Your guess is correct, I am checking the success and response times of websites using different proxy servers. I didn't try it but it's my understanding that it's almost impossible to pass data back to the DataTable which means the user would not be able to see the results appear row by row. If there is a way I would like to know. I'd rather have a loop. I like loops.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,242

    Re: Iterating though a DataTable and updating without "For Each"

    I don't know a good tutorial for Tasks. I just looked up a few examples, myself. They aren't terribly difficult, since you are basically just running a function. Tasks may or may not run on different threads (though in your case I'd say you could count on it), since that will be optimized.

    One thing about Tasks is that you can chain things together, including providing an action for the task to take upon completion. I would think that you could probably set it up to update a datatable without difficulty, but how that will appear in a control I couldn't say. One thing you wouldn't expect is for the rows to be updated in any particular order. They should be almost random.
    My usual boring signature: Nothing

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,972

    Re: Iterating though a DataTable and updating without "For Each"

    It's possible to pass data to/from a BGW to/from a datatable. What you CAN'T do from the BGW is update the UI... except for in the ReportProgress event or the Completed event.

    A couple lifetimes ago, we used a BGW to load data in chunks from a database, in the completed event, we'd add it to the datatable, refresh the bound grid, then spin up a new BGW, set the event handlers on it, then set it to get the next chunk of data (disclaimer, it was a thread we used, not a BGW directly... but the principle is the same).

    Just a thought.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Fanatic Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,020

    Re: Iterating though a DataTable and updating without "For Each"

    Thanks Shaggy. I'll be boning up on them today.

  7. #7

    Thread Starter
    Fanatic Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,020

    Re: Iterating though a DataTable and updating without "For Each"

    Techgnome what you described is what I had in mind. I'm going to look into tasks but I am keeping your idea in my back pocket too. How did you update the DataTable directly?

  8. #8

    Thread Starter
    Fanatic Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,020

    Re: Iterating though a DataTable and updating without "For Each"

    To all: The advice on Tasks and BGW are great and I appreciate the advice. But primarily I was hoping to get advice how to create a typed DataRow object that points ByRef to a specific row in the table. Does anyone have any advice on that?

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,242

    Re: Iterating though a DataTable and updating without "For Each"

    Don't worry about the ByRef part. ANY Datarow variable, typed, untyped, or purely fantastical, will be a reference type. This means that the variable will never hold the actual row, it will only hold a reference to the row, and that's exactly what you want. Having said that, I haven't worked with typed datatables, so I don't know how different they are from the non-typed variety. Specifically, I'm not sure what means you have to get at them, but here's what might work:

    Based on what you have said, it sounds like each row in the datatable is pretty much independent of the rest. By that I mean that you will want to take some action on each one, and the success or failure of the action for any one row doesn't depend on whether any of the other rows succeeded or failed. This would be particularly convenient, because then you might be able to do something like this:
    Code:
    Dim TheRowIWant = myDatatable.Select("someIdentifyingField = someValue").FirstOrDefault
    
    If TheRowIWant IsNot Nothing Then
     'Take action
    Else
     'Well, this really shouldn't happen.
    End If
    As long as you know the value, all of that code would be what I would put into a Task function. You'd pass in the value to use in the row selection (you'd have to concatenate it into that string in the Select statement), and you'd get back either 1 datarow, or Nothing. As long as you were ABSOLUTELY sure that the row would exist, you could skip the check of the returned row, too. In fact, in that case you could use .First rather than .FirstOrDefault, but .First will throw an exception if no row matches the selection criteria.
    My usual boring signature: Nothing

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