Solved

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

Posted on 2012-04-02
7
327 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

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. …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

688 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