views:

41

answers:

4

Hi,

I have an asp.net C# web app. In it I have a gridview. The gridview gets search results from a database. Sometimes there are a lot of results, and so I wanted to use paging. Here's what I tried:

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

For some reason, when I click on a page number, it shows me the EmptyDataText(There are no records to display). What code would work? Please help.

Thank you

A: 

You need to reassign your datasource to grdResults before the call to DataBind().

Chris Dwyer
Only if it isn't already declared in markup or in, say, a Load event. Do you have ViewState disabled? If not, I don't think you'd need to re-databind unless your data source only contains the displayed records (and omits the ones on other pages).
Yadyn
You're right. I don't need to rebind the data. But now, it doesnt change the the page until I click the page number twice...
zohair
A: 

EDIT: Remove these lines, they are not needed(maybe):

grdResults.PageIndex = e.NewPageIndex;
grdResults.DataBind();

The binding is dependent on your datasource, see here.

rick schott
You're right. I don't need to rebind the data. But now, it doesnt change the the page until I click the page number twice...
zohair
Can you post your gridview definition and how you bind it?
rick schott
A: 

check the link below

  1. http://asimsajjad.blogspot.com/2009/05/gridview-paging-and-sorting.html

hope that will help.

Asim Sajjad
+1  A: 

Hi,

Try assigning the datasource in NeedDataSource event.

Cheers.

Zinx