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...
kvprajapati 1,826 Posting Genius Team Colleague
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();
}
}
aniperiye 0 Newbie Poster
thanks friend...
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.