Help with binding gridview to an object containing a collection of objects
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Help with binding gridview to an object containing a collection of objects

Hybrid View

  1. #1
    Join Date
    Jan 2010
    Posts
    7

    Exclamation Help with binding gridview to an object containing a collection of objects

    This is a followup question to one I posted a couple of weeks ago. I have an Object (bootset), which I want to bind to my gridview. It contains 2 sets of chilld objects, which i must access. Here is the Boot/Bootset classes:

    Code:
    Option Explicit On
    Public Class Boot
        'Contains a head and shell
    #Region "Private Variables"
        Private _oHeadSet As New HeadSet
        Private _oShellSet As New ShellSet
        Private _iBootID As Integer
    #End Region
    
    #Region "Constructors"
        Public Sub New()
            _oHeadSet = New WQS_BusinessEntity.HeadSet
            Dim oHead As New Head
            _oHeadSet.Add(oHead)
            _oShellSet = New WQS_BusinessEntity.ShellSet
            Dim oShell As New Shell
            _oShellSet.Add(oShell)
    
    
        End Sub
        Public Sub New(ByVal oHeadSet As HeadSet, ByVal oShellSet As ShellSet, ByVal iBootID As Integer, ByVal iBootTypeID As Integer, ByVal iVesselID As Integer)
            _oHeadSet = New WQS_BusinessEntity.HeadSet
            _oShellSet = New WQS_BusinessEntity.ShellSet
            Dim oHead As New Head
            Dim oShell As New Shell
            _oHeadSet.Add(oHead)
            _oShellSet.Add(oShell)
            _iBootID = iBootID
          
        End Sub
    #End Region
    
    #Region "Public Properties"
        Public Property oHeadSet() As WQS_BusinessEntity.HeadSet
            Get
                Return _oHeadSet
            End Get
            Set(ByVal oValue As HeadSet)
                _oHeadSet = oValue
            End Set
        End Property
    
        Public Property oShellSet() As WQS_BusinessEntity.ShellSet
            Get
                Return _oShellSet
            End Get
            Set(ByVal oValue As ShellSet)
                _oShellSet = oValue
            End Set
        End Property
    
        Public Property iBootID() As Integer
            Get
                Return _iBootID
            End Get
            Set(ByVal iValue As Integer)
                _iBootID = iValue
            End Set
        End Property
     #End Region
    End Class
    
    
    Public Class BootSet
        Inherits Collections.ArrayList
    
        Public Shadows Function Add(ByVal oValue As Boot) As Integer
            If Not oValue Is Nothing Then
                MyBase.Add(oValue)
              
            End If
        End Function
    
        Default Public Shadows Property Item(ByVal iIndex As Integer) As Boot
            Get
                Return CType(MyBase.Item(iIndex), Boot)
    
            End Get
            Set(ByVal Value As Boot)
                MyBase.Item(iIndex) = Value
            End Set
        End Property
    
    End Class
    And the Head/HeadSet Classes:
    Code:
    Option Explicit On
    Public Class Head
        Inherits WQS_BusinessEntity.HeadSet
    #Region "Private Variables"
       
        Private _iHeadID As Integer
       
    #End Region
    
    #Region "Constructors"
        Public Sub New()
    
        End Sub
        Public Sub New(ByVal iHeadID As Integer)
          
            _iHeadID = iHeadID
          
    
        End Sub
    #End Region
    
    #Region "Public Properties"
       
        Public Property iHeadID() As Integer
            Get
                Return _iHeadID
            End Get
            Set(ByVal iValue As Integer)
                _iHeadID = iValue
            End Set
        End Property
        
    
    
    #End Region
    
    End Class
    Public Class HeadSet
        Inherits Collections.ArrayList
    
        Public Shadows Function Add(ByVal oValue As Head) As Integer
            If Not oValue Is Nothing Then
                MyBase.Add(oValue)
            End If
        End Function
    
        Default Public Shadows Property Item(ByVal iIndex As Integer) As Head
            Get
                Return CType(MyBase.Item(iIndex), Head)
    
            End Get
            Set(ByVal Value As Head)
                MyBase.Item(iIndex) = Value
            End Set
        End Property
    
    End Class
    And my Shell/ShellSet:
    Code:
    Option Explicit On
    Public Class Shell
        Inherits WQS_BusinessEntity.ShellSet
    
    #Region "Private Variables"
      
        Private _iShellID As Integer
       
    
    #End Region
    
    #Region "Constructors"
        Public Sub New()
    
        End Sub
        Public Sub New(ByVal iShellID As Integer)
            
            _iShellID = iShellID
           
        End Sub
    #End Region
    
    
    
    #Region "Public Properties"
       
        Public Property iShellID() As Integer
            Get
                Return _iShellID
            End Get
            Set(ByVal iValue As Integer)
                _iShellID = iValue
            End Set
        End Property
    
       
    #End Region
    End Class
    
    
    Public Class ShellSet
        Inherits Collections.ArrayList
    
        Public Shadows Function Add(ByVal oValue As Shell) As Integer
            If Not oValue Is Nothing Then
                MyBase.Add(oValue)
            End If
        End Function
    
        Default Public Shadows Property Item(ByVal iIndex As Integer) As Shell
            Get
                Return CType(MyBase.Item(iIndex), Shell)
    
            End Get
            Set(ByVal Value As Shell)
                MyBase.Item(iIndex) = Value
            End Set
        End Property
    
    End Class
    Notice that this Bootset Object contains one to many boots, each of which contains a headset and a shellset. a headset/shellset can contain one or more heads/shells. I need to Access (display) the properties in the contained obects, and do so in my gridview. Here is my code from my gridview, but as you can see i just dont know how to do what Im trying to accomplish.

    Code:
    <asp:GridView ID="grdBoots" runat="server"  AutoGenerateColumns="false"  >
    
    
    <AlternatingRowStyle CssClass="alt" />
    <Columns>
    <asp:TemplateField HeaderText="BootID" Visible="false">
    <%--**********************************Boot Number************************************************************************* --%>
    <HeaderTemplate>
    <asp:Label id="lblBootID" runat="server" Text='<%# Eval("iBootID") %>'></asp:Label>
    </HeaderTemplate>
    </asp:TemplateField>
    
    
    
    <%--**********************************Head Number*************************************************************************--%>
    <asp:TemplateField HeaderText="HeadID" Visible="false" >
    <ItemTemplate>
    <asp:HiddenField ID="hfHeadID" runat="server" Visible="False" Value='<%# Eval(".oHeadSet(0).iHeadID") %>' ></asp:HiddenField>
    </ItemTemplate>
    </asp:TemplateField>
    <%--**********************************Shell Number*************************************************************************--%>
    <asp:TemplateField HeaderText="ShellID" Visible="false" >
    
    <ItemTemplate>
    
    <asp:HiddenField ID="hfShellID" runat="server" Visible="False" Value='<%# Eval("oShellSet(0).iShellID") %>' ></asp:HiddenField>
    
    </ItemTemplate>
    <FooterTemplate>
    <asp:HiddenField ID="hfShellID" runat="server" Visible="False" Value='<%# Eval("oShellSet(0).iShellID") %>' ></asp:HiddenField>
    </FooterTemplate>
    </asp:TemplateField>
    
    </Columns>
    
    </asp:GridView>
    I could really use help on this one!

  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Help with binding gridview to an object containing a collection of objects

    asp.net...I am moving this thread there...
    ************************************
    seems as if you're looking for a hiearchichal control, and thait is not natively gridview, but dataGrid. Yoou could extend the first, or use the second
    here some samples that could help you (do not expect them to be perfect, you will have to adjust them to your needs):
    http://www.codeproject.com/KB/custom...tgridview.aspx
    http://www.codeproject.com/KB/webfor...rDataGrid.aspx
    Last edited by Cimperiali; February 10th, 2010 at 04:31 PM.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  3. #3
    Join Date
    Jan 2010
    Posts
    7

    Re: Help with binding gridview to an object containing a collection of objects

    Actually, that is not what I am trying to do. It does help me however as I am building an extended gridview myself, which does what those articles illustrate.

    With my current problem, I do not have child rows - just child objects. the Bootset is the parent object, and it contains a headset,a bootset, and a bootid. In my actual application there are other properties, but I simplified the problem for clarity. I want to display one row for each boot - which would also display the headid and the shellid for that boot.

    I have attached a zipped solution, which may make things clearer....it also contains code which is my efforts to solve the problem (the GetHeadID and GetShellID functions)
    Attached Files Attached Files

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center