Search code examples
c#asp.netpaginationdatagrid

Paging DataGrid Data using C#, Prev/Next don't work


Okay, I have looked everywhere and tried almost every suggestion that I could find and NOTHING has worked so far. D: Here's my problem:

I have a DataGrid with TemplateItemsthat the user has enteres into two TextBoxes that populates with data after the click of a button. My button takes two dates that the user enteres into two TextBoxes and pulls all entries between those dates from a DataBase. The entries are then displayed in the DataGrid. What I need this DataGrid to do is allow paging with 10 rows per page. All I need are Next and Prev links to go through the pages that contain data. The links work but the data doesn't change (Next doesn't go to the next page, the data remains the same). I know for a fact that there are more than 10 entries for Items between certain dates so I know the data should change based on what page it's on. Also, the Next buttons seems to be infinite. Why? Someone, please help me.

Now for some reason, when I get the data from the database, it starts out by getting all entries but then it only stores 10 (which is the amount that I want the pages to display at a time). The data never shows the rest that there should be... Why?! D':


Solution

  • So one of my co-workers ended up helping me with this one. Okay! This is the final result of my paging DataGrid. All of this code allowed me to get the number of items pulled from the database, set the VirtualItemCount (which also sets the number of pages that display), and page through the data. Please note that AllDataSources is a separate class that just pulls the data from the database in a way that works with my grid's set-up and CamSizerData is the name of the Table that contains all the data and is used to get the Count. If you have any other questions, please feel free to ask. :)

    DataGrid

    <asp:DataGrid ID="dgArchive" CssClass="data" AutoGenerateColumns="False" runat="server"
        AllowPaging="True" AllowCustomPaging="True" Visible="False" OnPageIndexChanged="dgArchive_PageIndexChanged">
        <Columns>
            <asp:TemplateColumn HeaderStyle-CssClass="infoHeaderDG" ItemStyle-CssClass="line">
                <HeaderTemplate>
                    Line</HeaderTemplate>
                <ItemTemplate>
                    <asp:Label ID="LineNumber" Text='<%# DataBinder.Eval(Container.DataItem, "LineNumber") %>'
                        runat="server" /></ItemTemplate>
                <HeaderStyle CssClass="infoHeaderDG"></HeaderStyle>
                <ItemStyle CssClass="line"></ItemStyle>
            </asp:TemplateColumn>
            <asp:TemplateColumn HeaderStyle-CssClass="infoHeaderDG" ItemStyle-CssClass="date">
                <HeaderTemplate>
                    Date</HeaderTemplate>
                <ItemTemplate>
                    <asp:Label ID="CreateDate" Text='<%# DataBinder.Eval(Container.DataItem, "CreateDate", "{0: MM/dd/yyyy}") %>'
                        runat="server" /></ItemTemplate>
                <HeaderStyle CssClass="infoHeaderDG"></HeaderStyle>
                <ItemStyle CssClass="date"></ItemStyle>
            </asp:TemplateColumn>
            <asp:TemplateColumn HeaderStyle-CssClass="infoHeaderDG" ItemStyle-CssClass="operator">
                <HeaderTemplate>
                    Operator</HeaderTemplate>
                <ItemTemplate>
                    <asp:Label ID="Operator" Text='<%# DataBinder.Eval(Container.DataItem, "Operator") %>'
                        runat="server" /></ItemTemplate>
                <HeaderStyle CssClass="infoHeaderDG"></HeaderStyle>
                <ItemStyle CssClass="operator"></ItemStyle>
            </asp:TemplateColumn>
            <asp:TemplateColumn HeaderStyle-CssClass="infoHeaderDG" ItemStyle-CssClass="product">
                <HeaderTemplate>
                    Product</HeaderTemplate>
                <ItemTemplate>
                    <asp:Label ID="Product" Text='<%# DataBinder.Eval(Container.DataItem, "ProdNumber") %>' runat="server" /></ItemTemplate>
                <HeaderStyle CssClass="infoHeaderDG"></HeaderStyle>
                <ItemStyle CssClass="product"></ItemStyle>
            </asp:TemplateColumn>
        </Columns>
        <PagerStyle Mode="NumericPages" PageButtonCount="5" />
    </asp:DataGrid>
    

    Code Behind

    protected void Page_Load(object sender, EventArgs e)
        {
            Page.MaintainScrollPositionOnPostBack = true;
        }
    
        protected void GetDateEntries_Click(object sender, EventArgs e)
        {
            dgArchive.Visible = true;
            using (Entities ent = new Entities(ConfigurationManager.ConnectionStrings["QCConnString"].ToString()))
            {
                DateTime start = Convert.ToDateTime(Start.Text);
                DateTime end = Convert.ToDateTime(End.Text).AddDays(1);
                AllDataSources ds = new AllDataSources();
                CamsizerData cd = new CamsizerData();
                IEnumerable<CamsizerData> led = cd.GetBySelectedDates(ent, start, end);
                int counter = led.Count();
                dgArchive.VirtualItemCount = counter;
    
                dgArchive.DataSource = ds.populateArchive(ent, start, end);
                dgArchive.DataBind();
            }
        }
    
        protected void dgArchive_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            if (source != null)
            {                
                dgArchive.CurrentPageIndex = e.NewPageIndex;
                Entities ent = new Entities(ConfigurationManager.ConnectionStrings["QCConnString"].ToString());
                DateTime start = Convert.ToDateTime(Start.Text);
                DateTime end = Convert.ToDateTime(End.Text).AddDays(1);
                AllDataSources ds = new AllDataSources();
    
                IEnumerable<object> recs = ds.populateArchive(ent, start, end);
                dgArchive.DataSource = recs.Skip(10 * e.NewPageIndex);
                dgArchive.DataBind();
            }
        }