0

hiiii i am aneesh...my doubt is how we can insert a repeater inside a gridview???
i want to put all repeater in one column of gridview. how it can achieve???? i am totally confused please give me a solution...

2
Contributors
2
Replies
6
Views
6 Years
Discussion Span
Last Post by aniperiye
0

Welcome.

You need to have two datasources - one for GridView and other for Repeater.


Have a look at this example.

Product class

public class Product
    {
        public int ProductID { get; set; }
        public string Name { get; set; }
    }

Category class

public class Category
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public  List<Product>  Items {get;set;}
        
    }

The class CategoryRepository returns List of Categories and products.

public class CategoryRepository
    {
        public static List<Category> GetCategories()
        {
            List<Category> coll = new List<Category>();
            coll.Add(new Category() { 
                 ID=1, 
                 Name="Category-1", 
                 Items=new  List<Product>()
                 {
                   new Product() { ProductID=101, Name="Product1"},
                   new Product() { ProductID=102, Name="Product2"},
                  }
            });
            coll.Add(new Category()
            {
                ID = 2,
                Name = "Category-2",
                Items = new List<Product>()
                 {
                   new Product() { ProductID=103, Name="Product3"},
                   new Product() { ProductID=104, Name="Product4"},
                  }
            });
            return coll;
        }
        public static List<Product> GetProducts(int catid)
        {
            return GetCategories().Find((p) => p.ID == catid).Items;
        }
    }

ASPX Markup (page1.aspx)

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:TemplateField HeaderText="Category">
                  <ItemTemplate>
                    <%#Eval("Name") %>
                    <asp:HiddenField ID="catid" runat="server" Value='<%# Eval("ID") %>' />
                  </ItemTemplate>
                </asp:TemplateField>
                
                <asp:TemplateField HeaderText="Products">
                   <ItemTemplate>
                       <asp:Repeater ID="Repeater1" runat="server">
                         <ItemTemplate>
                           <p>
                             <%# Eval("ProductID") %>
                             <%# Eval("Name") %>
                            </p> 
                         </ItemTemplate>
                       </asp:Repeater>
                   </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

Code-behind (page1.aspx.cs)

protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.RowDataBound += (sa, ea) =>
            {

                Repeater repeater = (Repeater)ea.Row.Cells[1].FindControl("Repeater1");
                if (repeater != null)
                {
                    HiddenField catid = (HiddenField)ea.Row.Cells[0].FindControl("catid");

                    repeater.DataSource = CategoryRepository.GetProducts(int.Parse(catid.Value));
                    repeater.DataBind();
                }
            };


        if (!IsPostBack)
        {
            GridView1.DataSource = CategoryRepository.GetCategories();
            GridView1.DataBind();
        }
    }
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.