Bind a string collection to a data grid

Discussion in 'ASP .Net' started by Pete Nelson, Oct 27, 2003.

  1. Pete Nelson

    Pete Nelson Guest

    Does anyone know of a way to bind a string collection to a datagrid?
    Basically, I have an object, CreditPlans, and a string collection,
    ModelNumbers. I'd like to bind the ModelNumbers collection to the
    datagrid, but because there isn't a custom object with properties, just
    a string object, I'm not sure how to reference it in the datagrid.

    FYI, I can enumerate through the collection...

    foreach(string modelNum in CreditPlan.ModelNumbers)
    {
    // code
    }


    But I'm not sure what would go in the datagrid....

    <asp:DataGrid Runat="server" ID="dgModels" AutoGenerateColumns="False">
    <Columns>
    <asp:BoundColumn DataField="???"/>
    </Columns>
    </asp:DataGrid>


    Any ideas? I can always create a custom ModelNumber object, but I'd
    like to avoid it if I can.

    --
    ===============================================================
    Pete "DVDTracker" Nelson | When the barrel of my Glock
    | meets Hoppes #9, it's like
    www.ecis.com/~weasel | a long-lost family reunion.
    ===============================================================
     
    Pete Nelson, Oct 27, 2003
    #1
    1. Advertising

  2. you could probably just convert your string contents to an array and bind
    directly to the array, as long as you could seperate out the items

    --
    Regards

    John Timney (Microsoft ASP.NET MVP)
    ----------------------------------------------
    <shameless_author_plug>
    Professional .NET for Java Developers with C#
    ISBN:1-861007-91-4
    Professional Windows Forms
    ISBN: 1861005547
    Professional JSP 2nd Edition
    ISBN: 1861004958
    Professional JSP
    ISBN: 1861003625
    Beginning JSP Web Development
    ISBN: 1861002092
    </shameless_author_plug>
    ----------------------------------------------

    "Pete Nelson" <> wrote in message
    news:...
    > Does anyone know of a way to bind a string collection to a datagrid?
    > Basically, I have an object, CreditPlans, and a string collection,
    > ModelNumbers. I'd like to bind the ModelNumbers collection to the
    > datagrid, but because there isn't a custom object with properties, just
    > a string object, I'm not sure how to reference it in the datagrid.
    >
    > FYI, I can enumerate through the collection...
    >
    > foreach(string modelNum in CreditPlan.ModelNumbers)
    > {
    > // code
    > }
    >
    >
    > But I'm not sure what would go in the datagrid....
    >
    > <asp:DataGrid Runat="server" ID="dgModels" AutoGenerateColumns="False">
    > <Columns>
    > <asp:BoundColumn DataField="???"/>
    > </Columns>
    > </asp:DataGrid>
    >
    >
    > Any ideas? I can always create a custom ModelNumber object, but I'd
    > like to avoid it if I can.
    >
    > --
    > ===============================================================
    > Pete "DVDTracker" Nelson | When the barrel of my Glock
    > | meets Hoppes #9, it's like
    > www.ecis.com/~weasel | a long-lost family reunion.
    > ===============================================================
     
    John Timney \(Microsoft MVP\), Oct 27, 2003
    #2
    1. Advertising

  3. Pete Nelson

    Pete Nelson Guest

    Pete Nelson <> wrote:

    ><asp:DataGrid Runat="server" ID="dgModels" AutoGenerateColumns="False">
    > <Columns>
    > <asp:BoundColumn DataField="???"/>
    > </Columns>
    ></asp:DataGrid>


    I found an answer.

    <asp:TemplateColumn HeaderText="Model Numbers">
    <ItemTemplate>
    <%# Container.DataItem.ToString() %>
    </ItemTemplate>
    </asp:TemplateColumn>



    --
    ===============================================================
    Pete "DVDTracker" Nelson | When the barrel of my Glock
    | meets Hoppes #9, it's like
    www.ecis.com/~weasel | a long-lost family reunion.
    ===============================================================
     
    Pete Nelson, Oct 27, 2003
    #3
    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. AD
    Replies:
    1
    Views:
    1,063
    Francis Shanahan
    Aug 23, 2003
  2. mr2_93
    Replies:
    1
    Views:
    3,648
    mr2_93
    Oct 2, 2005
  3. =?Utf-8?B?dmluZWV0YmF0dGE=?=

    How to bind data to DataGrid or Grid View

    =?Utf-8?B?dmluZWV0YmF0dGE=?=, Oct 16, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    552
    Eliyahu Goldin
    Oct 16, 2006
  4. Øyvind Isaksen
    Replies:
    1
    Views:
    1,013
    Øyvind Isaksen
    May 18, 2007
  5. gzinger

    How to bing a collection with sub-collection to grid

    gzinger, Jun 23, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    11
    Views:
    313
    Jeffrey Tan[MSFT]
    Jul 5, 2005
Loading...

Share This Page