?
Solved

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

Posted on 2012-04-02
7
Medium Priority
?
329 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 1200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

What is Waterfall Model? Waterfall model is the classic Software Development Life Cycle method practiced in software development process. As the name "waterfall" describes, this development is flowing downwards steadily like waterfall, i.e., procee…
Introduction This article discusses the Chain of Responsibility pattern, explaining What it is;Why it is; andHow it is At the end of this article, I hope you will be able to describe the use and benefits of Chain of Responsibility.  Backgrou…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
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…
Suggested Courses

777 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