Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Binding Generic Collections to an ASP.GridView

Binding Generic Collections to an ASP.GridView

Scheduled Pinned Locked Moved ASP.NET
csswpfwcfsysadmindocker
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Juvil John
    wrote on last edited by
    #1

    Hi, I have a generic object of type List<Items> with a sub collection of objects of type List<Descriptions>. I want to bind them to a gridview but not sure if it's possible, I have tried binding only the object of type List<Items> but if I try adding an ItemTemplate with another grid to try to bind the object of type List<Descriptions> i'm getting an error. says "it should implement IDatasource." something. my column structure in the grid would look like this.

    <Columns>
    <asp:BoundField DataField="Key" HeaderText="Key" SortExpression="Key" />
    <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
    <asp:TemplateField HeaderText="Descriptions" HeaderStyle-CssClass="headerstyle">
    <ItemTemplate>
    <asp:GridView ID="gv_descriptions" runat="server" DataSourceID='<%# DataBinder.Eval(Container.DataItem, "Descriptions") %>' AutoGenerateColumns="false"
    AllowPaging="true" EnableViewState="false" BorderStyle="None" GridLines="None" ShowEmptyTable="true" AllowSorting="true">
    <Columns>
    <asp:BoundField DataField="Description" HeaderText="ShortDescription" SortExpression="ShortDescription" />
    </Columns>
    </asp:GridView>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>

    N M 2 Replies Last reply
    0
    • J Juvil John

      Hi, I have a generic object of type List<Items> with a sub collection of objects of type List<Descriptions>. I want to bind them to a gridview but not sure if it's possible, I have tried binding only the object of type List<Items> but if I try adding an ItemTemplate with another grid to try to bind the object of type List<Descriptions> i'm getting an error. says "it should implement IDatasource." something. my column structure in the grid would look like this.

      <Columns>
      <asp:BoundField DataField="Key" HeaderText="Key" SortExpression="Key" />
      <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
      <asp:TemplateField HeaderText="Descriptions" HeaderStyle-CssClass="headerstyle">
      <ItemTemplate>
      <asp:GridView ID="gv_descriptions" runat="server" DataSourceID='<%# DataBinder.Eval(Container.DataItem, "Descriptions") %>' AutoGenerateColumns="false"
      AllowPaging="true" EnableViewState="false" BorderStyle="None" GridLines="None" ShowEmptyTable="true" AllowSorting="true">
      <Columns>
      <asp:BoundField DataField="Description" HeaderText="ShortDescription" SortExpression="ShortDescription" />
      </Columns>
      </asp:GridView>
      </ItemTemplate>
      </asp:TemplateField>
      </Columns>

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      Handle the RowDataBound event, find the nested GridView and bind List<Descriptions>.

      Navaneeth How to use google | Ask smart questions

      1 Reply Last reply
      0
      • J Juvil John

        Hi, I have a generic object of type List<Items> with a sub collection of objects of type List<Descriptions>. I want to bind them to a gridview but not sure if it's possible, I have tried binding only the object of type List<Items> but if I try adding an ItemTemplate with another grid to try to bind the object of type List<Descriptions> i'm getting an error. says "it should implement IDatasource." something. my column structure in the grid would look like this.

        <Columns>
        <asp:BoundField DataField="Key" HeaderText="Key" SortExpression="Key" />
        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
        <asp:TemplateField HeaderText="Descriptions" HeaderStyle-CssClass="headerstyle">
        <ItemTemplate>
        <asp:GridView ID="gv_descriptions" runat="server" DataSourceID='<%# DataBinder.Eval(Container.DataItem, "Descriptions") %>' AutoGenerateColumns="false"
        AllowPaging="true" EnableViewState="false" BorderStyle="None" GridLines="None" ShowEmptyTable="true" AllowSorting="true">
        <Columns>
        <asp:BoundField DataField="Description" HeaderText="ShortDescription" SortExpression="ShortDescription" />
        </Columns>
        </asp:GridView>
        </ItemTemplate>
        </asp:TemplateField>
        </Columns>

        M Offline
        M Offline
        mr_muskurahat
        wrote on last edited by
        #3

        dear while binding grid you must be doing some thing like.. grid.datasource = obj so this obj is not iEnumerable or idatasource might be you changing generic with something else.. type here obj.tolist() to make it ienumerable...

        Government Dyal Singh College Lahore.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups