Reputation: 742
I have below mark-up code in my ASPX page.
<asp:Repeater ID="rep" runat="server">
<ItemTemplate>
<asp:GridView ID="grdVw" runat="server" AutoGenerateColumns="true">
</asp:GridView>
</ItemTemplate>
</asp:Repeater>
Below C# code in my ASPX.CS page
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = new DataSet();
for (int i = 0; i < 5; i++)
{
ds.Tables.Add(getDT());
}
rep.DataSource = ds.Tables;
rep.DataBind();
}
private DataTable getDT()
{
DataTable dt = new DataTable();
dt.Columns.Add("One");
dt.Columns.Add("Two");
dt.Columns.Add("Three");
var row = dt.NewRow();
row["One"] = "Value1";
row["Two"] = "Value2";
row["Three"] = "Value3";
dt.Rows.Add(row);
return dt;
}
My intention is to have multiple gridviews depends on the number of tables of my dataset. After I run this code, I see nothing on the page. I don't know what wrong am I doing here?
Upvotes: 0
Views: 4768
Reputation: 35514
You can use the OnItemDataBound
event of the Repeater.
<asp:Repeater ID="rep" runat="server" OnItemDataBound="rep_ItemDataBound">
<ItemTemplate>
<asp:GridView ID="grdVw" runat="server"></asp:GridView>
</ItemTemplate>
</asp:Repeater>
Then in code begind find the nested GridView and bind the correct DataSet to it.
protected void rep_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
GridView gv = e.Item.FindControl("grdVw") as GridView;
gv.DataSource = ds.Tables[e.Item.ItemIndex];
gv.DataBind();
}
Upvotes: 1
Reputation: 1677
This is what I can suggest you:
//after these lines
rep.DataSource = ds.Tables;
rep.DataBind();
//add below lines:
int count = 1;
foreach (RepeaterItem item in rep.Items)
{
var grd = item.FindControl("grdVw") as GridView;
grd.DataSource = ds.Tables[count - 1];
grd.DataBind();
count++;
}
Hope this helps.
Upvotes: 0