Tuesday, March 2, 2010

The GridView 'GridViewID' fired event PageIndexChanging which wasn't handled Problem

Technology
- ASP.NET

Problem Description
If you set AllowPaging="true" on a GridView control without using a DataSourceControl DataSource (i.e. SqlDataSource, ObjectDataSource), you will find an error when changing the page on the GridView control:
The GridView 'GridViewID' fired event PageIndexChanging which wasn't handled.


Solution
All what you need here is to handele PageIndexChanging event , then reassign the new page index to the grid page index and finally rebind your grid again as it will loses the datasource binding.
protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gridView.PageIndex = e.NewPageIndex;
//rebind the Data grid here after assigning the datasource again
}

Finally
You are supposed the find the same problem on sorting as well.
So you have to set the Sort Property of the grid with both direction and expression in the handler of Sorting event of the grid.
Sorting event will be raised by calling the sort method of the grid in server side after handling the click event of each link in the grid header.

Welcome to Questions and Comments ...

No comments:

Post a Comment