1

I am using PageIndexChanging event for handling GridView paging in C#. But don't know how can to use PageSize/PageNumber/PageCount there. In other word my code is forced to return all data always. Note following code:

protected void grdList_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
        grdList.PageIndex = e.NewPageIndex;
        grdList.DataSource = MyGetData();
        grdList.DataBind();
}

Now how can I use real paging in this code?

Notice that MyGetData has an overload that get PageIndex and PageSize too.

UPDATE

I have set PageSize and enabled AllowPaging too. I know if I use declarative data binding I should supply GridView with count of all data. Question is how can can use count in this method.

UPDATE 2 It seems that such a thing I need is not possible, refer to Problem with Efficient Gridview paging without datasource control

Community
  • 1
  • 1
Afshar Mohebi
  • 10,479
  • 17
  • 82
  • 126

4 Answers4

1

Efficient paging in GridView needs count of data, otherwise GridView loads all data in each page. As there is no way to tell GridView what is the count of data when not using DataSource controls, it's impossible to have efficient paging in GridView without having DataSource control. For more info go to this link and this link.

Community
  • 1
  • 1
Afshar Mohebi
  • 10,479
  • 17
  • 82
  • 126
0

You can set the PageSize in the GridView control.

Shaun Mason
  • 787
  • 4
  • 14
0

you need to set PageSize="10"

see in this link: http://www.dotnetspider.com/resources/1249-Grid-View-Paging-Sorting.aspx

anishMarokey
  • 11,279
  • 2
  • 34
  • 47
  • he can set PageSize to any value - it doesnt have to be 10 - infact a control can be made to set the pagesize dynamically if necessary – stack72 Sep 23 '10 at 12:47
  • sample code in your given link loads all data whenever you navigate across pages in pager. A real paging mechanism loads only data related to a single page. A real paging has a very high positive impact on application performance. – Afshar Mohebi Sep 23 '10 at 14:03
0

If your MyGetData method already accepts pageindex and pagesize, then all you'd need is:

protected void grdList_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    grdList.PageIndex = e.NewPageIndex;
    grdList.DataSource = MyGetData(e.NewPageIndex, grdList.PageSize);
    grdList.DataBind();
}

but this seems a bit too simplistic so I'm probably missing something here.

PhilPursglove
  • 12,511
  • 5
  • 46
  • 68