views:

666

answers:

3

Hi,

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 funtion 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? Thank you

+2  A: 

Instead of grd.DataBind() in your paging method call bindGrid(). Or better use some built-in 'business objects' for data binding like ObjectDataSource

Tadas
+1  A: 

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

}
TheVillageIdiot
A: 

The simplest way to do paging is to set AllowPaging="yes" in your GridView, and don't do anything in the code-behind. The GridView will page through its dataset on its own. Do NOT rebind.

That's fine for small datasets, but not so good for a phone book. As Tadas said, ObjectDataSource is the way to go for handling paging yourself, on server side. With ObjectDataSource, you won't DataBind at all; the controls handle that. However, you do need to supply a Select method (usually static) on an object, and it needs to have parameters for first row and page size (set these as attributes on the ObjectDataSource). You will allso need to implement a Count method to return the full dataset size; otherwise paging won't work. Using an ObjectDataSource this way, you shift the burding of paging to your data layer, and the page will load much faster.

Cylon Cat