Solved

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

Posted on 2012-04-02
7
315 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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 question got me thinking... (http://www.experts-exchange.com/questions/28707487/GLOBALS.html) Why shouldn't we use Globals? This is a simple question without a simple answer.  How do you explain these concepts to a programmer w…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

759 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

18 Experts available now in Live!

Get 1:1 Help Now