DataGrid Paging doesnt page!

D

Dee

Hi
The paging numbers of my DataGrid dont actually page. What can be the cause?
Everyting else seems to work.
Thanks
Dee
 
D

Dee

Thanks Scott,
I dodnt know I needd to handle that event.
It workd now. I added:

DataGrid1.CurrentPageIndex = e.NewPageIndex
DataGrid1.DataBind()

It's intersting that the paging works without thr DataBind() call but have
to click on the page TWICE!
 
J

Jeebu

HI
set the property of datagrid to AllowPaging="True"
then
on the grid_page index changed event
write the code

grid.CurrentPageIndex = e.NewPageIndex ;

and then write the code for connecting to the datbase
 
S

Scott M.

You must ALWAYS call the databind method after making any kind of change to
the datagrid that affects what data the grid is showing.

The reason you'd have to click the pager twice without the extra databind
call is that the grid uses ViewState to remember, not only the data that is
was showing, but also other "state" information such as what page of data
was showing, what sort order was in effect, what row was being edited or was
selected, etc.

Without the databind call, the grid is bound to its data in Page_Load
(that's where you do have a databind call, right?) and the first click sets
the page correctly but doesn't update the grid with the new page of data
because no databind call took place AFTER setting the page to show, the
second click causes the grid to bind to its data in Page_Load as well, but
now the grid is "remembering" via ViewState what page of data to display
BEFORE the databind call takes place.

In general, your page load event handler should look like this:

datagrid.datasource = someDataSource
If Not IsPostBack Then
datagrid.databind
End If

and then at the end of EACH and EVERY datagrid event handler where the data
would change in some way (sortCommand, PageIndexChanged, EditCommand,
DeleteCommand, SortCommand, CancelCommand)

datagrid.databind
 
D

Dee

Wow
Great explanation! Thanks very much.
Dee

Scott M. said:
You must ALWAYS call the databind method after making any kind of change to
the datagrid that affects what data the grid is showing.

The reason you'd have to click the pager twice without the extra databind
call is that the grid uses ViewState to remember, not only the data that is
was showing, but also other "state" information such as what page of data
was showing, what sort order was in effect, what row was being edited or was
selected, etc.

Without the databind call, the grid is bound to its data in Page_Load
(that's where you do have a databind call, right?) and the first click sets
the page correctly but doesn't update the grid with the new page of data
because no databind call took place AFTER setting the page to show, the
second click causes the grid to bind to its data in Page_Load as well, but
now the grid is "remembering" via ViewState what page of data to display
BEFORE the databind call takes place.

In general, your page load event handler should look like this:

datagrid.datasource = someDataSource
If Not IsPostBack Then
datagrid.databind
End If

and then at the end of EACH and EVERY datagrid event handler where the data
would change in some way (sortCommand, PageIndexChanged, EditCommand,
DeleteCommand, SortCommand, CancelCommand)

datagrid.databind
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top