Solved

VB.net 4.0 Using for each loop through an object to display in gridview control

Posted on 2012-04-02
7
316 Views
Last Modified: 2012-04-04
Hello Experts!!  I am fairly new to VB.net and need a simple solution to loop through data in an object to display a list of rows in a gridview control or the best option to achieve this.

Is the gridview control the best way to achieve this?
0
Comment
Question by:codeJen
  • 2
  • 2
  • 2
  • +1
7 Comments
 

Expert Comment

by:LBmarco
ID: 37796589
hello,
gridview is a good solution.
the most important when you want to display data in a gridview is to create a datasource that is either an IListSource, IEnumerable, or IDataSource.

Try first this :
 GridView1.DataSource = your_object_list
 GridView1.DataBind()

if it is not working, you will have to create a datatable with one column per object property, propagate data from object to datarow and bind the datatable to the gridview.

I hope it helps
Marc
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37796601
Winforms? ASP.NET? What sort of object is it? Gridview is useful when dealing with many objects of same type such as table rows.
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 37797466
TypeCast the object to its type and place it on the datagridview, e.g if the object is an anonymous type and you know that it contains integers, simply use CInt(YourObject) when adding it to the grid. (others are CDbl, CStr etc)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:codeJen
ID: 37797723
I guess I should have asked for some code examples.  GridView Control is the way I am going to go.

I need to some how use logic like this..but i don't think an array will work in a gridview control.  

Dim oMLAgentData As MLAgentData
 Dim arrayBuilder As New System.Text.StringBuilder
        For Each oMLAgentData In oMLOrg.OrgData
            If arrayBuilder.Length > 0 Then
                arrayBuilder.Append(",")
                arrayBuilder.Append(vbCrLf)
            End If
            arrayBuilder.Append(oMLAgentData.toJSArray())
        Next
        arrayBuilder.Append(vbCrLf)
        arrayString = arrayBuilder.ToString

Open in new window

0
 
LVL 17

Expert Comment

by:nepaluz
ID: 37797765
We do not have the definitions / structure of your object, thus we are really limited as to how much more advice / code suggestion we can give.
0
 

Accepted Solution

by:
LBmarco earned 300 total points
ID: 37804976
hello,
I think that the best way to display your object list properties into a gridview is to create a datatable mapping your MLAgentData.

something like that :
     
   Function Convert_to_dt(ByVal infos As MLAgentData) As DataTable
            Dim dt As New DataTable
            dt.Columns.Add(New DataColumn("ID", System.Type.GetType("System.Int32")))
            dt.Columns.Add(New DataColumn("Title", System.Type.GetType("System.String")))
            'add any other column we need
            For Each infoo As InfoObject In infos
                Dim dr As DataRow = dt.NewRow()
                dr.Item("ID") = infoo.ID
                dr.Item("Title") = infoo.Title
                'add any other businesslogic for the other columns
                dt.Rows.Add(dr)
            Next
            Return dt

        End Function

Open in new window



this type of function can be very usefull because :
1- you create an output that can be directly usable by the griedview
2- all your business logic and data manipulation will be host in this function

after, you can use this datatable as the datasource of your gridview :

GridView1.DataSource = Convert_to_dt(oMLAgentData)
 GridView1.DataBind()

I hope it helps
Regards
Marc
0
 

Author Closing Comment

by:codeJen
ID: 37806394
This is exactly what I was looking for.  Thank you LBMarco
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Fluent Interface Design Pattern You can use the Fluent Interface (http://en.wikipedia.org/wiki/Fluent_interface) design pattern to make your PHP code easier to read and maintain.  "Fluent Interface" is an object-oriented design pattern that r…
Introduction Many of the most common information processing tasks require sorting data sets.  For example, you may want to find the largest or smallest value in a collection.  Or you may want to order the data set in numeric or alphabetical order. …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

861 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

23 Experts available now in Live!

Get 1:1 Help Now