Posted on 2006-05-03
Last Modified: 2010-04-23

A general question on how to "FILL A STRUCTURE with DATA ON WIN FORM LOAD EVENT", with some detail code included.  I am new to VB2005.NET.  I have created a FORM and figured out how to LOAD a DATAGRID CONTROL from an MS ACCESS Table.  Most of the code to do this was auto-generated and all I did was tweak control/object properties or Event Handlers.  I got what I wanted, a DATABOUND GRID CONTROL to AUTOFILL on a WIN FORM.

What I really want to do now is to cycle thru the MS ACCESS TABLE DATA and read each ROW ELEMENT in to a RECORD STRUCTURE and create an STRUCTURE ARRAY.  This would occur when the FORM IS LOADED.


Public Structure DONOR
    Public strDSTATUS As String
    Public lngDNISADDRBKnum As Long
    Public strDENAME As String
    Public sngDFTE As Single
    Public sngDVBAL As Single
    Public sngDSBAL As Single
    Public lngDECLASS
    Public datDDate As Date
End Structure

This is the current FORM LOAD EVENT CDX:

Private Sub frmDONORS_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'TODO: This line of code loads data into the 'PSCLVACCTm0DataSet.STAFF' table. You can move, or remove it, as needed.

    End Sub

Specifically, my primary question is how the following line of VB2005.NET Code


can be modified to read each ROW/ELEMENT(FIELD/COLUMN) sequentially in the TABLE and ASSIGN IT to the corresponding ROW STRUCTURE ELEMENT.

Basically, A FOR EACH STATEMENT to READ the MS_ACCESS TABLE would be the easiest way to loop thru the TABLE, but I'm not sure that'll work, being a VB2005.NET Newbie.  My experience tells me, I may have to write code(DO UNTIL) to check for a EOF Condition.

My secondary question then and I'm only looking for general direction and concept development here, is I then want to selectively use STRUCTURE ARRAY ELEMENTS to load /populate a GRID CONTROL and DISLAY the DATA on the FORM when it is LOADED? So, can I create/populate the DATAGRID CONTROL with the STRUCTURE ARRAY DATA on the FORM, when it is LOADED  and after I have created the STRUCTURE ARRAY?

Any guidance and assistance would be greatly appreciated.


Mike O
Question by:mhotto
    LVL 34

    Expert Comment

    Specifically, my primary question is how the following line of VB2005.NET Code


    can be modified to read each ROW/ELEMENT(FIELD/COLUMN) sequentially in the TABLE and ASSIGN IT to the corresponding ROW STRUCTURE ELEMENT.

    My primary answer is "it can't".  A TableAdapter fills a table in (so far as the user/application sees) one uninterrupted process.  You have two choices.  One is to use a DataReader rather than a TableAdapter.  That reads single record by single record and allows you to do things with that single record before the next one is brought.  The other is to continue to use the TableAdapter and then read through the filled Table row by row to create your Structure and add it to your array.  That is, you would in fact be "read[ing] each ROW/ELEMENT(FIELD/COLUMN) sequentially in the TABLE and ASSIGN IT to the corresponding ROW STRUCTURE ELEMENT" but you would be doing it by adding to, rather than modifying, the line of code that you quote.

    On your secondary question, and remembering that you are looking for general guidance, my comment would be that you would have, in the DataTable that the TableAdapter above fills, a tool specifically built to display data (selected or otherwise) in a datagrid on a form.  There may be other reasons why you want to move values from cells in a row in a datatable into elements in a structure and then put the structure that corresponds to each row in an array.  But, it seems to me, you are essentially just replicating what a datatable already is.  A datarow is, in effect, a "structure": a collection of name/value "elements".  And a datatable is, in effect, an array of those structures.

    Given that a datatable can be bound to a datagrid very simply, whereas binding an array of structures to a datagrid would be much more complicated, I wonder why?


    Author Comment


    Thanks for taking the time, to read my question and formulate an answer.  Especially, helpful, since I'm a Newbie @ VB_05. And sorry, for the slow response.  Been handling other BOSS FIRE MISSIONS.

    The reference to the "DATA READER" Object wasn't something that occurred to me, even though, I had come across it during tutorials, along the idea of datastreams.

    To answer your question, as pertains why go to what seems like a complicated data structure coding problem, when the datagrid is analogous to a programmer defined structure.  I think a problem documentation LINK

     of what my application is doing and what I'm trying to accomplish is alot easier, than a lot of verbose text in this form.

    Any additional guidance/comments/questions/suggestions would be appreciated, particulalry since I'm new to VB_05.  But, the points are yours.


    LVL 34

    Accepted Solution


    when the datagrid is analogous to a programmer defined structure

    That's not quite what I meant.  The datagrid is no more than a display and user-entry mechanism.  The point that I was trying to make was that a "dataTABLE is analogous to a programmer defined structure".  And my view then was - and it remains now, having looked at the link you provided - that there is unlikely to be anything that you can do with a programmer defined structure that is filled from a DataTable that you cannot do (much more easily) with the DataTable itself.  

    As I said last time, binding a DataTable to a DataGrid or DataGridView is a doddle.  You can do it in the IDE GUI or with one line of code

       MyDataGrid.DataSource = MyDataTable

    But binding a Structure to a DataGrid or DataGridView has, effectively, to be self-programmed.

    If you want the DataTable to be available to other forms you can either declare it in a Module which has scope for those other forms or pass a reference to it to those other forms: which is just what you would have to do with a Structure.  If there is a relationship (or relationships) between different DataTables - e.g. the many to one POOL to DONOR relationship mentioned on the link - you can declare that relationship on the DataTables themselves and use it - perhaps with DataViews - to fill DataGrids or DataGridViews with whatever data, filtered and sorted in whatever orders you want, directly from those DataTables without having to put it into a Structure first.

    I realise that a lot of what I am saying comes from having worked with data in VB.NET for a long time.  I'm familiar with what it can do and how to make use of it.  If you are happy working with Structures then it may be more efficient to continue to do so rather than having to grapple with unfamiliar objects and syntax.  But I'd certainly recommend you to at least have a look in the help file at DataRelations and at DataViews as, I think, they might save you a lot of lines of code.


    Author Comment

    Appreciate the guidance.  What I've accomplished so, far is with the MS provided tool-set. And based on your experience and in my case no experience with VB_05 .NET.  It's probably best I go with that, rather than roll my own.

    Thanks again.


    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now