If you set AllowPaging="true" or AllowSorting="true" on a GridView control without using a DataSourceControl DataSource (i.e. SqlDataSource, ObjectDataSource), you will run into the following errors:
When changing the page on the GridView control:
The GridView 'GridViewID' fired event PageIndexChanging which wasn't handled.
When clicking a column name to sort the column on the GridView control:
The GridView 'GridViewID' fired event Sorting which wasn't handled.
As a result of not setting the DataSourceID property of the GridView to a DataSourceControl DataSource, you have to add event handlers for sorting and paging.
The more that I think of it, you would need a method only if you want to ensure that the displayed data is fresh and not stale from the current data source. However, this does bring up the downside to interface paging, which
is filling the entire data source (i.e. DataTable, DataSet) each time you do that, which can get expensive depending on the amount of rows returned.
Thanks a lot!. I was using dataset to bind my GridView control instead of dataTable. I changed to use DataTable and now it works.
Another problem I am having is that after sorting, different data gets displayed on the current screen since now they are in different order - I am also using pagination on these pages. There are anyway to make sort just the current page or after clicking
on sort, the same data displays on the current screen sorted?
The sort worked after that. How did you avoid having to use ViewState. I'm fairly new at this but I'm worried about a performance problem if there are a large number of records.
You shouldn't have to put it in ViewState. Revert back to the following, but add the first line that I have below and let me know what the output it produces is:
protected void gridView_Sorting(object sender,
GridViewSortEventArgs e)
{
Response.Write(gridView.DataSource.GetType()); //Add this line
DataTable m_DataTable = gridView.DataSource
as DataTable;
if (m_DataTable !=
null)
{
DataView m_DataView = new
DataView(m_DataTable);
m_DataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
DataTable table = gvResults.DataSource
as DataTable;
if (table ==
null)
{
lblSearchResults.Text = lblSearchResults.Text +
" TABLE STILL CAME BACK NULL";
}
It blew up when attempting to GetType saying "Object reference not set to an instance of an object.". No matter though, I saved the table to cache after initially populating the grid which works well. When I call the sort I look for the cached table, sort
it using the DataView code and rebind the grid. If the cached table has already dropped I go ahead and recreate it and then sort. If, for any reason, it still isn't there the user just doesn't get a sort! One question though, for some reason my sort direction
seems to always be ascending. I thought it was supposed to automatically toggle back and forth.
StrongTypes
All-Star
30801 Points
6013 Posts
ASPInsiders
HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource
Jan 24, 2006 05:55 PM|LINK
If you set AllowPaging="true" or AllowSorting="true" on a GridView control without using a DataSourceControl DataSource (i.e. SqlDataSource, ObjectDataSource), you will run into the following errors:
When changing the page on the GridView control:
The GridView 'GridViewID' fired event PageIndexChanging which wasn't handled.
When clicking a column name to sort the column on the GridView control:
The GridView 'GridViewID' fired event Sorting which wasn't handled.
As a result of not setting the DataSourceID property of the GridView to a DataSourceControl DataSource, you have to add event handlers for sorting and paging.
<asp:GridView ID="gridView" OnPageIndexChanging="gridView_PageIndexChanging" OnSorting="gridView_Sorting" runat="server" />
private string ConvertSortDirectionToSql(SortDirection sortDirection)
{
string newSortDirection = String.Empty;
switch (sortDirection)
{
case SortDirection.Ascending:
newSortDirection = "ASC";
break;
case SortDirection.Descending:
newSortDirection = "DESC";
break;
}
return newSortDirection;
}
protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gridView.PageIndex = e.NewPageIndex;
gridView.DataBind();
}
protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dataTable = gridView.DataSource as DataTable;
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
gridView.DataSource = dataView;
gridView.DataBind();
}
}
GridView
ChrisOngsuco
Member
319 Points
66 Posts
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource
Feb 07, 2006 07:13 AM|LINK
protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
// Add here your method for DataBinding
BindGridControl();
gridView.PageIndex = e.NewPageIndex;
Without the databinding method you won't get the paged result.gridView.DataBind();
}
Chris Adrian S. Ongsuco
URL: http://www.chrisongsuco.net
StrongTypes
All-Star
30801 Points
6013 Posts
ASPInsiders
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource
Feb 07, 2006 12:35 PM|LINK
protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
// Add here your method for DataBinding
BindGridControl();
gridView.PageIndex = e.NewPageIndex;
gridView.DataBind();
}
Without the databinding method you won't get the paged result.
I've used that code as-is (the code in my original post), and it works perfectly. No need for a custom method.
StrongTypes
All-Star
30801 Points
6013 Posts
ASPInsiders
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource
Feb 07, 2006 07:09 PM|LINK
The more that I think of it, you would need a method only if you want to ensure that the displayed data is fresh and not stale from the current data source. However, this does bring up the downside to interface paging, which is filling the entire data source (i.e. DataTable, DataSet) each time you do that, which can get expensive depending on the amount of rows returned.
netNewBee
Member
250 Points
149 Posts
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource
Feb 10, 2006 06:24 PM|LINK
Hi,
The paging function works for me but no the sorting one. It always fails at this line:
if (m_DataTable != null)
because m_DataTable is always NULL according to the VS Debugger. Does anyone have any ideas on I can get this to work?
Thanks a lot!
netNewBee :-)
StrongTypes
All-Star
30801 Points
6013 Posts
ASPInsiders
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource
Feb 10, 2006 06:35 PM|LINK
What are you using as a data source (i.e. DataTable, DataSet, etc.) to bind to the GridView?
netNewBee
Member
250 Points
149 Posts
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource
Feb 10, 2006 06:44 PM|LINK
Thanks a lot!. I was using dataset to bind my GridView control instead of dataTable. I changed to use DataTable and now it works.
Another problem I am having is that after sorting, different data gets displayed on the current screen since now they are in different order - I am also using pagination on these pages. There are anyway to make sort just the current page or after clicking on sort, the same data displays on the current screen sorted?
netNewBee :-)
robbyram
Member
10 Points
2 Posts
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource
Feb 14, 2006 08:42 PM|LINK
Can you please explain a little further. I used a DataTable to bind to my grid.
gvResults.DataSource = ds.Tables[0];
gvResults.DataBind();
That worked fine, however the only way I was able to make the sort work using your example I had to hold onto the datasource via ViewState
ViewState["gvResults_DataSource"] = gvResults.DataSource;
After having done that I was able to modify the first line of the sorting routine to
DataTable dt = (DataTable)ViewState["gvResults_DataSource"];
The sort worked after that. How did you avoid having to use ViewState. I'm fairly new at this but I'm worried about a performance problem if there are a large number of records.
Thanks in advance.
StrongTypes
All-Star
30801 Points
6013 Posts
ASPInsiders
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource
Feb 15, 2006 03:02 AM|LINK
You shouldn't have to put it in ViewState. Revert back to the following, but add the first line that I have below and let me know what the output it produces is:
protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
{
Response.Write(gridView.DataSource.GetType()); //Add this line
DataTable m_DataTable = gridView.DataSource as DataTable;
if (m_DataTable != null)
{
DataView m_DataView = new DataView(m_DataTable);
m_DataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
gridView.DataSource = m_DataView;
gridView.DataBind();
}
}
robbyram
Member
10 Points
2 Posts
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource
Feb 15, 2006 07:57 PM|LINK
I added the following code:
lblSearchResults.Text = gvResults.DataSource.GetType().ToString();
DataTable table = gvResults.DataSource as DataTable; if (table == null){
lblSearchResults.Text = lblSearchResults.Text +
" TABLE STILL CAME BACK NULL";}
It blew up when attempting to GetType saying "Object reference not set to an instance of an object.". No matter though, I saved the table to cache after initially populating the grid which works well. When I call the sort I look for the cached table, sort it using the DataView code and rebind the grid. If the cached table has already dropped I go ahead and recreate it and then sort. If, for any reason, it still isn't there the user just doesn't get a sort! One question though, for some reason my sort direction seems to always be ascending. I thought it was supposed to automatically toggle back and forth.
Thanks for your help.