Need Help Iterating Through DataSet to Return Data...

This is probably a real stupid question... I have a dataset that gets created programatically (no connected database, no TableAdapter, no datagrid - just a dataset used to store a ton of records temporarily in memory).

I have the dataset bound to a couple listboxes to filter information and everything works exactly as I want it to.

Now, I'm creating a report based off the information in the dataset. The stupid question is...

If I want to query the dataset to get back a single item for the report, how do I do it

To make it simple to understand, the dataset has 6 columns and probably 150 records. If I know that I want to retrieve the data for a single item in column 4 where I know that column 3 has a unique identifier called ABCD in that particular record, how do I get it Let's say I just want to say:

Dim TempVar as String = <that single "cell's" data in Col 4 where Col 3 = ABCD >

I would think this should be an easy or common task, but I can't find anything on how to do it.

HELP!!

-- Jim



Answer this question

Need Help Iterating Through DataSet to Return Data...

  • Julia Semenova

    Thanks for the input, but doesn't that only look at the row and not at the columns It doesn't seem to return what I'm looking for.

    It seems like it should be something more like:

    Dim TempVar as String

    For each row as DataRow in Table.Rows

    If COLUMN(3) = "ABCD" then

    TempVar = table.COLUMN(4)

    exit for

    end if

    Next

    But I can't seem to get it to work. What's your thoughts (or anyone's thoughts) on this

    Thanks again for your help.

    -- Jim


  • DevJohan

    Wow... OK, you're right, I was completely not understanding how it works. It makes a little more sense now. I was able to take what you were saying and make it work.

    So, for instance:

    Dim dr As DataRow = BPI_Dataset.Tables("BPI_Table").Rows.Find("Machine Name")
    Dim dr2 As String = dr("Data")
    MsgBox(dr2)

    That gives me what I'm after.

    Thanks for your help, shakalama and ReneeC!

    -- Jim


  • lotofu

    You're welcome Jimmy...

    There's something that has been occuring to me as far as performance is concerned.

    Using the column name as a string gives you more dynamism for sure. I use enumerated column indexes which has less dynamism but it has save a lot of cpu cycles per record search.



  • Halvo

    I will keep that in mind as I get this going. Thanks again for your help.

    -- Jim


  • Mathieu Cupryk

    hi i guess what confuse you is the column name , to get data from the row you have to use the column name or column index for example

    value = myrow("columnstringName")

    you can use the column name or conlumn index actualy its not the column that you retrieve its the cell that share the column name and this row something like coordinates , you can't use the columns to search as in your post  because the columns diffention is just for datatype restrictions (integer, string ,unique , ..... etc), the rows that hold data , you can simply use

    value = myrow(columnindex)

    so in your case to find particular row you can itterate through your table  as what Reenec said or you can use something like this

    dim dr as datarow  = table1.Rows.Find("columnName = value")

    or even you can get array of rows if you want to if its not unique value

    dim dr() as datarow  = table1.Rows.Find("columnName = value")

    to read from those rows

    string Name = dr("Column1Name")

    integer age = dr("Column2Name")

    note that column index is zero based

    hope this help



  • Nidal-Fouad-Hajj-Youssef

     

    This is really easy!!!

     

    Dim TempVar as String

    For each row as DataRow in Table.Rows

    If row(3) = "ABCD" then

    tempvar = table.row(4)

    exit for

    end if

    Next

     

    Now then if you have known row limits and can save time in a sequential search a slight modification is possible:

     

    For I as integer = StartPos to Endpos

    If table.row(3) = "ABCD" then

    tempvar = row(4)

    exit for

    end if

    next

     

    And there are no stupid questions. But there are ways that you can help youself and others at the same time. I know we become harried in projects, but coul you take a moment and read the suggestions at the top One of the requests for optimizing a response is to make a clear problem statement in your topic. That way people with expertise won't read your question by chance, they'll be drawn to it right away. Also in the future database will know clearly how to sort your clearly stated title. So there's nore than one reason for the request. 



  • tarunkapur

    you are welcome

  • HS-Immeronline

    I guess the way I picture it is almost like an Excel document since it's only one table.

    So, it would look something like this...

    _____________________________________________
    |   ID  |   TopLevel   |   Topic      |   Data   |   Data2   |  Recs     |
    _____________________________________________
    |         |                      |    BCDE   |    2345 |                  |               |
    _____________________________________________
    |         |                      |    ABCD   |    1234 |                  |               |
    _____________________________________________
    |         |                      |    CDEF   |    3456 |                  |               |
    _____________________________________________

    I know that the row that has ABCD as the topic has the data I'm looking for.  So in this case, if I can iterate through the DataSet and find out which row has ABCD as the Topic (each topic is unique), then I need to get the entry for the Data column in that same row.  So with the above table, it should return 1234 as the response.

    Hope that helps!  Thanks again.

    -- Jim


  • Need Help Iterating Through DataSet to Return Data...