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?
 
MlandaTConnect With a Mentor Commented:
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
 
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

 
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
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.