views:

105

answers:

5

Hi all,

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 http://stackoverflow.com/questions/2518968/problem-with-efficient-gridview-paging-without-datasource-control

A: 

You can set the PageSize in the GridView control.

ShaunLMason
@ShaunLMason, I have no problem with `PageSize`. Please see my update.
afsharm
A: 

you need to set PageSize="10"

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

anishmarokey
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
PaulStack
so only i given a link too
anishmarokey
@anishmarokey, 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.
afsharm
A: 

Hope this link may help you. link text

programmer4programming
@Hemanth_Laxmi, I read it. But its GridView is using a DataSource while I'm setting the DataSource in CodeBehind.
afsharm
Ok.I thought it may help you.
programmer4programming
A: 

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
@PhilPursglove: I used this way. This works good. But pager does not work. Because GridView does not know what is the count of data.
afsharm
@PhilPursglove: see my second UPDATE.
afsharm
A: 

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.

afsharm