How Do I Bind List Property to GridView??

Hello EE,

So I asked this question: HERE

and I received a great solution to my issue.  However, I'm now confused on how to retrieve that same list collection and bind it to a gridview control.

Basically,  how do I get all of the Box properties out of Boxes and bind to gridview. along with properties that are exposed through Shipping class?

The code below is the experts post which solved my initial question and I'd reposting this code to continue with the example for better understanding.
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim oShipping As New Shipping()
        oShipping.Boxes.Add(New Box("a"))
    End Sub
End Class


Public Class Shipping
    Private _boxes As List(Of Box)
    Public Property Boxes() As List(Of Box)
        Get
            Return _boxes
        End Get
        Set(ByVal value As List(Of Box))
            _boxes = value
        End Set

    End Property

    Public Sub New()
        _boxes = New List(Of Box)
    End Sub

End Class


Public Class Box
    Private type As String
    Public Property BoxType() As String
        Get
            Return type
        End Get
        Set(ByVal value As String)
            type = value
        End Set
    End Property

    Public Sub New(ByVal type As String)
        BoxType = type
    End Sub

End Class

Open in new window

sk1922Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
It really depends on what you would like to achieve with the GridView.  The usual approach is to use a BoundField, but you can also use a TemplateField with an Eval expression.

Repeater example:


ASP.NET ItemTemplates, EVAL() and embedding dynamic values into controls
http://www.west-wind.com/Weblog/posts/5364.aspx

<asp:repeater id="rptSpecials" runat="server">
   <itemtemplate>
       <asp:HyperLink runat="server"
              NavigateUrl='~/item.aspx?sku=<%# Eval("sku") %>'
              Text='<%# Eval("specialhd") %>'/>
   </itemtemplate>
</asp:repeater>

Open in new window

0
MlandaTCommented:
This worked for me
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm2.aspx.vb" Inherits="TestGlobalization.WebForm2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    
    </div>
    </form>
</body>
</html>

Open in new window

Public Partial Class WebForm2
    Inherits System.Web.UI.Page

    Private ship As New Shipping

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        ship.Boxes.Add(New Box("BigBox"))
        ship.Boxes.Add(New Box("SmallBox"))
        ship.Boxes.Add(New Box("HappBox"))
        ship.Boxes.Add(New Box("MediumBox"))
        ship.Boxes.Add(New Box("SadBox"))

        GridView1.DataSource = ship.Boxes
        GridView1.DataBind()

    End Sub

End Class

Open in new window

0
sk1922Author Commented:
Hello and thank you for your post!!

Well what I'm trying to do is instantiate a Shipping object then iterate through the Boxes list to display each Box property in a gridview.

I can easily use a dataset and just map that to the gridview but I wanted to use the Object.  My page has fields that I'd like to map my properties exposed through the Shipping class and then dig into the Boxes property and loop them (if a loop is even needed) to display each Box...

I hope that make sense.
0
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Bob LearnedCommented:
You don't need:

1) a DataSet/Table, a List(Of Box) is sufficient

2) For loops

That last suggestion is about using the AutoGenerateColumns="true" setting for the GridView.  If you need more customization, then you need to define columns (the usual approach).
0
MlandaTCommented:
Alternatively, you can define the columns inthe grid as you wish. Here is a sample showing a Databound field, a Template Field, and autogenerated fields


        <asp:GridView ID="GridView1" runat="server">
            <Columns>
                <asp:TemplateField HeaderText="OurTemplate Column">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("BoxType") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("BoxType") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="BoxType" HeaderText="BoxType DataBound Column" />
            </Columns>
        </asp:GridView>

Open in new window

0
MlandaTCommented:
I have not used a dataset anywhere. Note that we just declare:

Private ship As New Shipping

Open in new window


and then bind directly to ship.Boxes which is if type List(of Box)
GridView1.DataSource = ship.Boxes
GridView1.DataBind()

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sk1922Author Commented:
I'm going to try this out.  

One question...

the last post :

GridView1.DataSource = ship.Boxes
GridView1.DataBind()

this is saying that my GV will bind only the properties exposed by ship.Boxes

Is there a way to include properties of the Shipping Class in the GV Bind?  Or will I need to create another Class which is almost like a Display that would include the Boxes and the Shipping properties?
0
sk1922Author Commented:
Also,

Since my data is retrieved from a DB won't I need to loop (for each) through each and add to my list when I new up a Shipping object?

ex.  ---

Public Sub New(byval someID as Integer)

Dim ds as DataSet = GetMyShippingDetails(someID)

Me.ShippingProperty1 = dr("SOME_COL")
Me.ShippingProperty2 = dr("SOME_COL")
_boxes = New List(Of Box)
For each dr as DataRow in ds.Tables(0).Rows
_boxes.Add(New Box("some value", "some value", "some value"))
Next

End Sub
0
sk1922Author Commented:
so I was able to bind the List of Box.  However, I am having difficulties binding the Properties which are not part of the Box class which are available through the Shipping class.


any ideas?
0
MlandaTCommented:
you can only bind properties of the box class. if you want the ship kevel as well you have to nest the grids. maybe these links can help:

http://www.codeguru.com/columns/vb/article.php/c12647
http://www.codeproject.com/KB/aspnet/MultiNestMDGridview.aspx
http://www.highoncoding.com/Articles/230_Nested_GridView_Grouping_and_Displaying_SubTotals_and_GrandTotals.aspx

but otherwise you can just google the topic: asp.net nested gridviews


and yes... there is no need for any loops since we are using databinding

good luck.
0
sk1922Author Commented:
>> and yes... there is no need for any loops since we are using databinding

When I don't add the loop to my constructor, it will not add my Boxes nor bind to the gridview.  Am I missing something here?? And is this not necessary??

Shipping Class
----

Public Sub New(byval shippingID as integer)

dim ds as dataset = getmyrequest(shippingID)
_boxes = New List(Of Box)
For Each dr as DataRow in ds.Tables(0).Rows
_boxes.Add(New Box("x", "x", "x"))
Next

end sub
0
MlandaTCommented:
oh sorry.... I see we are talking about different loops. there are several stages to this:

1. loading data (get data from database, manipulate it as needed - for which you are looping to add it to the list)
2. binding data to the display control (we are using data binding, sometimes one can loop and add items to the grid one by one - and this is the loop which I was saying we dont need)

So we had just misunderstoond each other.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.