Binding a GridView available inside a Repeater

by Jagadish Pulakhandam on 9/20/2011 11:59:21 AM
Rated 0 from 0 votes
Brief: Demonstrates on binding a Repeater which contains GridView inside it.
Posted to: Data Binding using ObjectDataSource in ASP.NET
Add to DiggAdd to del.icio.usAdd to FURLAdd to RedditAdd to YahooAdd to BlinklistAdd to GoogleAdd to ma.gnoliaAdd to ShadowsAdd to Technorati

Demonstrates the following:
  • Create Business Entities which return master/detail data
  • Create Business Factory Methods to fill data
  • Configure ObjectDataSource to work with Business Factory Methods
  • Create a simple ItemTemplate for Repeater which includes GridView Inside it
  • Bind Repeater to ObjectDataSource (implies binding to GridView as well)
  • Binding without writing any code-behind
Screen shot:



Source Code:

01.using System;
02.using System.Collections.Generic;
03.using System.Linq;
04.using System.Text;
05. 
06.namespace NorthwindDataAccess.Models
07.{
08.    public class Product
09.    {
10.        public int ProductID { get; set; }
11.        public string ProductName { get; set; }
12.        public int CategoryID { get; set; }
13.        public double UnitPrice { get; set; }
14.    }
15. 
16.    public class Category
17.    {
18.        public int CategoryID { get; set; }
19.        public string CategoryName { get; set; }
20.    }
21. 
22.    public class CategoryProducts : Category
23.    {
24.        public List<Product> Products { get; set; }
25.    }
26.}

01.using System;
02.using System.Collections.Generic;
03.using System.Linq;
04.using System.Text;
05. 
06.using System.Data;
07.namespace NorthwindDataAccess.Factories
08.{
09.    public class CategoryFactory
10.    {
11.         public static List<Models.CategoryProducts> GetCategoriesAndProducts()
12.        {
13.            //you can also write this in a more efficient way (by bringing all data at once with a single round-trip)
14.            //but this is simply for demonstration
15.            DataTable dtResult = DbHelper.GetResultSet("SELECT CategoryID, CategoryName FROM Categories");
16.            List<Models.CategoryProducts> lst = dtResult
17.                                                .AsEnumerable()
18.                                                .Select(dr => new Models.CategoryProducts()
19.                                                {
20.                                                    CategoryID = Convert.ToInt16(dr["CategoryID"]),
21.                                                    CategoryName = dr["CategoryName"].ToString()
22.                                                })
23.                                                .ToList();
24. 
25.            foreach (Models.CategoryProducts oCat in lst)
26.            {
27.                oCat.Products = ProductFactory.GetListByCategory(oCat.CategoryID);
28.            }
29. 
30.            return lst;
31.        }
32.    }
33.}

01.<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="NorthwindWebSite.WebForm1" %>
02. 
03.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
05.<head runat="server">
06.    <title></title>
07.</head>
08.<body>
09.    <form id="form1" runat="server">
10.    <div>
11.        <table>
12.            <asp:Repeater ID="Repeater1" runat="server" DataSourceID="ObjectDataSource1">
13.                <ItemTemplate>
14.                    <tr>
15.                        <td>
16.                            Category: <asp:Label ID="Label1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "CategoryName") %>'></asp:Label>
17.                        </td>
18.                    </tr>
19.                    <tr>
20.                        <td style="padding-left: 50px">
21.                            <asp:GridView ID="GridView1" runat="server"
22.                                DataSource='<%# ((NorthwindDataAccess.Models.CategoryProducts)Container.DataItem).Products %>'>
23.                            </asp:GridView>
24.                        </td>
25.                    </tr>
26.                </ItemTemplate>
27.                <SeparatorTemplate>
28.                    <tr>
29.                        <td>
30.                            <hr />
31.                        </td>
32.                    </tr>
33.                </SeparatorTemplate>
34.            </asp:Repeater>
35.        </table>
36.        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetCategoriesAndProducts"
37.            TypeName="NorthwindDataAccess.Factories.CategoryFactory"></asp:ObjectDataSource>
38.    </div>
39.    </form>
40.</body>
41.</html>



Rated 0 from 0 votes ( login  to rate)
DotnetKicks DotnetKicksDe DotNetShoutout

Attachments / Source Code
You need to Login or Join for FREE to download the following

Video/Screen Recording (may not have audio narration/annotations)
You need to Login or Join for FREE to download the following

Discussion
No comments yet!