C# – gridview paging using dataset

asp.netcdatasetgridviewpagination

I have an ASP.NET web app (C#) where I get some information from a datasource and display it in the the gridview. I wanted to enable paging, but for some reason, paging doesn't work. I researched a little online, and I found that paging is done differently if a dataset is used. When I click on a page number, it refreshes, and says that there are no records to display. I call this function in the click function of a button:

        bindGrid(cmd);

Here's my bind method:

    private void bindGrid(OracleCommand comm)
    {
        OracleDataAdapter adapter = new OracleDataAdapter(comm);
        DataSet ds = new DataSet();

        ds.Tables.Add("Results");
        adapter.Fill(ds.Tables["Results"]);

        grd.DataSource = ds;
        grd.DataBind();
    }

Paging Method:

    protected void grdResults_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grd.PageIndex = e.NewPageIndex;
        grd.DataBind();
    }

How am I supposed to do paging with a dataset? Can someone help please?

Best Answer

You also need to fetch the data :)

So instead of this:

protected void grdResults_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    grd.PageIndex = e.NewPageIndex;
    grd.DataBind();
}

you should use:

protected void grdResults_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    grd.PageIndex = e.NewPageIndex;

    //Create command
    bindGrid(comm);

}
Related Topic