SiSan
SiSan

Reputation: 55

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':

Upvotes: 0

Views: 3975

Answers (3)

SiSan
SiSan

Reputation: 55

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();
        }
    }

Upvotes: 0

James Johnson
James Johnson

Reputation: 46047

You need to rebind the grid each time you change the page. Here's an example that outlines what you need to do:

<asp:DataGrid ID="DataGrid1" runat="server" OnPageIndexChanged="DataGrid1_PageIndexChange" ...>

Code-behind:

protected void DataGrid1_PageIndexChange(object sender, DataGridPageChangedEventArgs e)
{
    DataGrid1.CurrentPageIndex = e.NewPageIndex;
    BindDataGrid();
}

private void BindDataGrid()
{
    DataGrid1.DataSource = GetSomeData();
    DataGrid1.DataBind();

    int total = dt.Rows.Count;
    int pSize = grdJobs.PageSize;
    int pIndex = grdJobs.CurrentPageIndex;

    if (total < pSize) 
        pSize = total;

    int start = (pSize * pIndex) + 1;
    int end = (start + pSize) - 1;

    if (end > total) 
        end = total;

    lblTotalResults.Text = String.Format("Displaying {0}-{1} Of {2}", start, end, total);        
}

Upvotes: 0

Icarus
Icarus

Reputation: 63956

Set New Page Index before you set datasource.

protected void dgArchive_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{

    if (source != null)
    {
        dgArchive.CurrentPageIndex = e.NewPageIndex;
        JSP_Extrusion_QCEntities ent = new JSP_Extrusion_QCEntities(ConfigurationManager.ConnectionStrings["QCConnString"].ToString());
        DateTime start = Convert.ToDateTime(Start.Text);
        DateTime end = Convert.ToDateTime(End.Text).AddDays(1);
        AllDataSources ds = new AllDataSources();

        dgArchive.DataSource = ds.populateArchive(ent, start, end);
        dgArchive.DataBind();

    }
}

Also, why do you wire up your events on Page_Load? You don't need to do that if you do it on the markup.

These 3 lines:

    GetDateEntries.Click += new EventHandler(GetDateEntries_Click);
    dgArchive.VirtualItemCount = 200;
    dgArchive.PageIndexChanged += new DataGridPageChangedEventHandler(dgArchive_PageIndexChanged);

Should be declared in the markup. Here's how you register on PageIndexChanged for your grid from the markup.

<asp:DataGrid ID="dgArchive" CssClass="data" AutoGenerateColumns="False" runat="server" AllowPaging="true" 
PageSize="10" EnableViewState="true" AllowCustomPaging="true" Visible="false"
OnPageIndexChanged="dgArchive_PageIndexChanged"
>

Here's an example from MSDN.

Upvotes: 1

Related Questions