How To Create a page able DataGrid within a Repeater

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewIndideRepeater.aspx.cs"
Inherits="GridViewIndideRepeater" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
  <div>
      <asp:Repeater ID="ItemsList" OnItemDataBound="Item_Bound" runat="server">
          <HeaderTemplate>
              <table>
                  <caption>
                      List of Categories
                  </caption>
          </HeaderTemplate>
          <ItemTemplate>
              <tr>
                  <td>
                      <%# DataBinder.Eval(Container.DataItem, "Category") %>
                  </td>
              </tr>
              <tr>
                  <td>
                      <asp:DataGrid ID="dataGrid1" runat="server" BorderColor="black" GridLines="Both"
                          HeaderStyle-BackColor="#aaaadd" PagerStyle-Mode="NextPrev"
                          PagerStyle-HorizontalAlign="Center"
                          PagerStyle-CssClass="GridPager" PagerStyle-Position="Bottom"
                          PagerStyle-PageButtonCount="25"
                          PagerStyle-NextPageText="next" PagerStyle-PrevPageText="Prev"
                          PagerStyle-BackColor="gainsboro"
                          OnPageIndexChanged="ChangeGridPage" PageSize="5"
                          AllowPaging="True"
                          AutoGenerateColumns="false">
                          <Columns>
                              <asp:BoundColumn Visible="False" ReadOnly="true" DataField="ID" />
                              <asp:BoundColumn HeaderText="Qty" ReadOnly="true" DataField="Qty" />
                              <asp:BoundColumn HeaderText="Description" ReadOnly="true" DataField="Description" />
                              <asp:BoundColumn HeaderText="Price" DataField="Price" DataFormatString="{0:c}">
                                  <ItemStyle HorizontalAlign="right"></ItemStyle>
                              </asp:BoundColumn>
                          </Columns>
                      </asp:DataGrid>
                  </td>
              </tr>
          </ItemTemplate>
          <FooterTemplate>
              </table>
          </FooterTemplate>
      </asp:Repeater>
  </div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class GridViewIndideRepeater : System.Web.UI.Page
{

   public ICollection CreateDataSource()
   {
       // Create sample data for the DataList control.
       DataTable dt = new DataTable();
       DataRow dr;
       // Define the columns of the table.
       dt.Columns.Add(new DataColumn("ID", typeof(Int32)));
       dt.Columns.Add(new DataColumn("Category", typeof(string)));
       // Populate the table with sample values.
       int i;
       for (i = 1; i <= 5; i++)
       {

           dr = dt.NewRow();
           dr[0] = i;
           dr[1] = "Category " + i.ToString();
           dt.Rows.Add(dr);

       }

       DataView dv = new DataView(dt);
       return dv;

   }

   public DataTable CreateDGDataSource()
   {
       // Create sample data for the DataList control.
       DataTable dt = new DataTable();
       DataRow dr;
       int i;
       int y;
       // Define the columns of the table.
       dt.Columns.Add(new DataColumn("ID", typeof(int)));
       dt.Columns.Add(new DataColumn("Qty", typeof(int)));
       dt.Columns.Add(new DataColumn("Price", typeof(int)));
       dt.Columns.Add(new DataColumn("Description", typeof(string)));
       //Make some rows and put some sample data in
       for (y = 1; y <= 5; y++)
       {
           for (i = 1; i <= 10; i++)
           {
               dr = dt.NewRow();
               dr[0] = y;
               dr[1] = i * y;
               dr[2] = i * y;
               dr[3] = "Item " + y + "_" + i;
               //add the row to the datatable
               dt.Rows.Add(dr);
           }
       }

       return dt;
   }

   public ICollection CreateDGDataSource(int CategoryID)
   {
       DataView dv = new DataView(CreateDGDataSource(), "ID=" + CategoryID, null,
           DataViewRowState.CurrentRows);
       return dv;

   }

   public void Page_Load(object sender, EventArgs e)
   {

       // Load sample data only once, when the page is first loaded.
       if (!IsPostBack)
       {

           ItemsList.DataSource = CreateDataSource();
           ItemsList.DataBind();

       }

   }
   public void BindDG(DataGrid dg, int CategoryID)
   {

       dg.DataSource = CreateDGDataSource(CategoryID);
       dg.DataBind();
   }
   public void ChangeGridPage(object sender, DataGridPageChangedEventArgs objArgs)
   {
       int CategoryID = int.Parse(((DataGrid)sender).Items[0].Cells[0].Text);
       ((DataGrid)sender).CurrentPageIndex = objArgs.NewPageIndex;
       BindDG((DataGrid)sender, CategoryID);
   }


   public void Item_Bound(object sender, RepeaterItemEventArgs e)
   {

       if (e.Item.ItemType == ListItemType.Item | e.Item.ItemType == ListItemType.AlternatingItem)
       {

           // Retrieve the Label control in the current DataListItem.
           DataGrid dg = (DataGrid)e.Item.FindControl("DataGrid1");
           if (dg == null)
           {
               Response.Write("DataGrid not found");
           }
           else
           {
               object CategoryID = ((DataRowView)((RepeaterItem)e.Item).DataItem)["ID"];
               dg.DataSource = CreateDGDataSource((int)CategoryID);
               dg.DataBind();
           }


       }

   }


}
Reactions

Post a Comment

0 Comments

Close Menu