Bind GridView to generic custom object List collection

Discussion in 'ASP .Net' started by Steven Baggs, Oct 17, 2005.

  1. Steven Baggs

    Steven Baggs Guest

    Hi!,

    I have defined a List<T> like this List<Container>
    Container class holds my Item object and it's state as a Enum.

    Container.Item
    Container.State

    I would like to bind it to GridView.

    gridview1.DataSourde = list;
    gridview1.DataBind();

    Problem is that I can't get it to show the data. I try:
    <asp:BoundField DataField="Item.Name" HeaderText="Name" />

    but I'm getting an error saying that Item doesn't exist.

    Right now I'm using DataSet but I would like to use my business object's
    collection.

    Best regards,

    Steven
     
    Steven Baggs, Oct 17, 2005
    #1
    1. Advertising

  2. Hi Steven,

    When I bind custom collections to GridViews, I use the following:
    <asp:BoundField DataField="State" HeaderText="State"></asp:BoundField>

    or:

    <asp:TemplateField HeaderText="State">
    <ItemTemplate>
    <%# DataBinder.Eval(Container, "DataItem.State") %>
    </ItemTemplate>
    </asp:TemplateField>

    The above assumes that your custom collection contains an has a
    "State" property that returns a string.

    Hope this helps.

    Tod Birdsall, MCSD for .NET
    blog: http://tod1d.blogspot.com
     
    Tod Birdsall, MCSD for .NET, Oct 18, 2005
    #2
    1. Advertising

  3. Steven Baggs

    Joe Dotnet Guest

    Thanks for the reply. I have to check is, in my case, Item an property. It
    might be a public variable.


    "Tod Birdsall, MCSD for .NET" <> wrote in message
    news:...
    > Hi Steven,
    >
    > When I bind custom collections to GridViews, I use the following:
    > <asp:BoundField DataField="State" HeaderText="State"></asp:BoundField>
    >
    > or:
    >
    > <asp:TemplateField HeaderText="State">
    > <ItemTemplate>
    > <%# DataBinder.Eval(Container, "DataItem.State") %>
    > </ItemTemplate>
    > </asp:TemplateField>
    >
    > The above assumes that your custom collection contains an has a
    > "State" property that returns a string.
    >
    > Hope this helps.
    >
    > Tod Birdsall, MCSD for .NET
    > blog: http://tod1d.blogspot.com
    >
     
    Joe Dotnet, Oct 18, 2005
    #3
  4. Steven Baggs

    Steven Baggs Guest

    well it wasn't a property. Now I can access the 'Item' object.
    How can I access further - the ExpirationDate property of the Item object?

    is something like that valid?
    <asp:BoundField DataField="Item.ExpirationDate" HeaderText="Expiration Date"
    />

    Right now I'm getting:

    A field or property with the name 'Item.ExpirationDate' was not found on the
    selected data source.


    "Tod Birdsall, MCSD for .NET" <> wrote in message
    news:...
    > Hi Steven,
    >
    > When I bind custom collections to GridViews, I use the following:
    > <asp:BoundField DataField="State" HeaderText="State"></asp:BoundField>
    >
    > or:
    >
    > <asp:TemplateField HeaderText="State">
    > <ItemTemplate>
    > <%# DataBinder.Eval(Container, "DataItem.State") %>
    > </ItemTemplate>
    > </asp:TemplateField>
    >
    > The above assumes that your custom collection contains an has a
    > "State" property that returns a string.
    >
    > Hope this helps.
    >
    > Tod Birdsall, MCSD for .NET
    > blog: http://tod1d.blogspot.com
    >
     
    Steven Baggs, Oct 18, 2005
    #4
  5. Steven Baggs

    xhead Guest

    It sounds like this should work, no?

    <asp:BoundField DataField="ExpirationDate" HeaderText="Expiration
    Date"/>

    or maybe this:

    <asp:TemplateField HeaderText="Expiration Date">
    <ItemTemplate>
    <%# Eval("ExpirationDate") %>
    </ItemTemplate>
    </asp:TemplateField>

    Mike

    Steven Baggs wrote:
    > well it wasn't a property. Now I can access the 'Item' object.
    > How can I access further - the ExpirationDate property of the Item object?
    >
    > is something like that valid?
    > <asp:BoundField DataField="Item.ExpirationDate" HeaderText="Expiration Date"
    > />
    >
    > Right now I'm getting:
    >
    > A field or property with the name 'Item.ExpirationDate' was not found on the
    > selected data source.
    >
    >
    > "Tod Birdsall, MCSD for .NET" <> wrote in message
    > news:...
    > > Hi Steven,
    > >
    > > When I bind custom collections to GridViews, I use the following:
    > > <asp:BoundField DataField="State" HeaderText="State"></asp:BoundField>
    > >
    > > or:
    > >
    > > <asp:TemplateField HeaderText="State">
    > > <ItemTemplate>
    > > <%# DataBinder.Eval(Container, "DataItem.State") %>
    > > </ItemTemplate>
    > > </asp:TemplateField>
    > >
    > > The above assumes that your custom collection contains an has a
    > > "State" property that returns a string.
    > >
    > > Hope this helps.
    > >
    > > Tod Birdsall, MCSD for .NET
    > > blog: http://tod1d.blogspot.com
    > >
     
    xhead, Oct 18, 2005
    #5
  6. Steven Baggs

    Steven Baggs Guest

    ExpirationDate is a property of the Item object which is a property of a
    ShoppingMetaElement object :)

    I found this working Text= '<%# Eval("Item.ExpirationDate") %>' in the
    TemplateField.

    Seems impossible in DataBound column.

    BTW: MSDN beta documentation of ASP.NET has a lot of bugs.


    "xhead" <> wrote in message
    news:...
    > It sounds like this should work, no?
    >
    > <asp:BoundField DataField="ExpirationDate" HeaderText="Expiration
    > Date"/>
    >
    > or maybe this:
    >
    > <asp:TemplateField HeaderText="Expiration Date">
    > <ItemTemplate>
    > <%# Eval("ExpirationDate") %>
    > </ItemTemplate>
    > </asp:TemplateField>
    >
    > Mike
    >
    > Steven Baggs wrote:
    >> well it wasn't a property. Now I can access the 'Item' object.
    >> How can I access further - the ExpirationDate property of the Item
    >> object?
    >>
    >> is something like that valid?
    >> <asp:BoundField DataField="Item.ExpirationDate" HeaderText="Expiration
    >> Date"
    >> />
    >>
    >> Right now I'm getting:
    >>
    >> A field or property with the name 'Item.ExpirationDate' was not found on
    >> the
    >> selected data source.
    >>
    >>
    >> "Tod Birdsall, MCSD for .NET" <> wrote in message
    >> news:...
    >> > Hi Steven,
    >> >
    >> > When I bind custom collections to GridViews, I use the following:
    >> > <asp:BoundField DataField="State" HeaderText="State"></asp:BoundField>
    >> >
    >> > or:
    >> >
    >> > <asp:TemplateField HeaderText="State">
    >> > <ItemTemplate>
    >> > <%# DataBinder.Eval(Container, "DataItem.State") %>
    >> > </ItemTemplate>
    >> > </asp:TemplateField>
    >> >
    >> > The above assumes that your custom collection contains an has a
    >> > "State" property that returns a string.
    >> >
    >> > Hope this helps.
    >> >
    >> > Tod Birdsall, MCSD for .NET
    >> > blog: http://tod1d.blogspot.com
    >> >

    >
     
    Steven Baggs, Oct 18, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. A Traveler

    Cant bind Repeater to Custom Collection

    A Traveler, Oct 19, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    7,488
    A Traveler
    Oct 19, 2004
  2. Luis Esteban Valencia
    Replies:
    1
    Views:
    650
    Joe Fallon
    Jul 3, 2005
  3. Øyvind Isaksen
    Replies:
    1
    Views:
    987
    Øyvind Isaksen
    May 18, 2007
  4. Jonathan Wood
    Replies:
    6
    Views:
    40,699
    Jonathan Wood
    Feb 9, 2008
  5. A Traveler

    Cant bind Repeater to Custom Collection

    A Traveler, Oct 19, 2004, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    203
    A Traveler
    Oct 19, 2004
Loading...

Share This Page