Solved

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

Posted on 2012-04-02
7
323 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

830 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