CodeVerge.Net Beta


   Explore    Item Entry   Register  Login  
Microsoft News
Asp.Net Forums
IBM Software
Borland Forums
Adobe Forums
Novell Forums




Can Reply:  No Members Can Edit: No Online: Yes
Zone: > NEWSGROUP > Asp.Net Forum > general_asp.net.faq_frequently_asked_questions Tags:
Item Type: NewsGroup Date Entered: 1/24/2006 6:55:57 PM Date Modified: Subscribers: 0 Subscribe Alert
Rate It:
NR
XPoints: N/A Replies: 185 Views: 237 Favorited: 0 Favorite
186 Items, 10 Pages 1 2 3 4 5 6 7 8 9 10 |< << Go >> >|
StrongTypes
Asp.Net User
HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource1/24/2006 6:55:57 PM

0

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 sortDireciton)
{
   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();
   }
}


Ryan Olshan
Microsoft MVP, ASP.NET
Blog | Group | Website | Strong Coders Community

How to ask a question
ChrisOngsuco
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource2/7/2006 8:13:44 AM

0

StrongTypes wrote:

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



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.

Regards,

Chris Adrian S. Ongsuco
URL: http://www.chrisongsuco.net
StrongTypes
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource2/7/2006 1:35:04 PM

0

ChrisOngsuco wrote:


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.


Ryan Olshan
Microsoft MVP, ASP.NET
Blog | Group | Website | Strong Coders Community

How to ask a question
StrongTypes
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource2/7/2006 8:09:41 PM

0

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.


Ryan Olshan
Microsoft MVP, ASP.NET
Blog | Group | Website | Strong Coders Community

How to ask a question
netNewBee
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource2/10/2006 7:24:33 PM

0

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!


Thank you very much for your help!

netNewBee :-)
StrongTypes
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource2/10/2006 7:35:24 PM

0

What are you using as a data source (i.e. DataTable, DataSet, etc.) to bind to the GridView?


Ryan Olshan
Microsoft MVP, ASP.NET
Blog | Group | Website | Strong Coders Community

How to ask a question
netNewBee
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource2/10/2006 7:44:10 PM

0

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?


Thank you very much for your help!

netNewBee :-)
robbyram
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource2/14/2006 9:42:24 PM

0

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
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource2/15/2006 4:02:05 AM

0

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();
   }
}


Ryan Olshan
Microsoft MVP, ASP.NET
Blog | Group | Website | Strong Coders Community

How to ask a question
robbyram
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource2/15/2006 8:57:46 PM

0

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.

StrongTypes
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource2/15/2006 9:46:20 PM

0

robbyram wrote:
 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 catching that. It was something I overlooked originally. Like with the DataGrid, you'll have to place it in the ViewState to get that to work. I'll update that code tonight so ascending/descending sort works in both directions.


Ryan Olshan
Microsoft MVP, ASP.NET
Blog | Group | Website | Strong Coders Community

How to ask a question
MikeOtown
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource2/18/2006 9:24:23 PM

0

I also find that the GridView.DataSource is Nothing in the GridView Sorting event.  I'm using a DataTable as the DataSource in the Page Load event.

Is this because Sorting fires before DataBound?  Also, why does this work for the original poster of this code?  Is he secretly storing the DataTable somewhere and not telling us?

StrongTypes
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource2/18/2006 10:16:15 PM

0

Hi Mike,

The code does work and I'll be posting a live working example with source code in about 1/2 hour.

Ryan


Ryan Olshan
Microsoft MVP, ASP.NET
Blog | Group | Website | Strong Coders Community

How to ask a question
StrongTypes
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource2/18/2006 11:08:09 PM

0

Here's the live working example with source code: GridView Sorting/Paging w/o a DataSourceControl DataSource

I had to tweak the code a little to make sorting work in both directions.

Enjoy.
Ryan


Ryan Olshan
Microsoft MVP, ASP.NET
Blog | Group | Website | Strong Coders Community

How to ask a question
Blake05
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource2/19/2006 8:04:38 AM

0

Ryan, I've been checking out your sites and your work, and also reading most of your posts. I would just like to say keep up the good work because I know its helped a lot of people on these forums.

-Blake Niemyjski
Blog - Website: windowscoding.com
StrongTypes
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource2/19/2006 9:09:42 AM

0

Thanks for the kind words. I remember those who helped me when I needed help, so I like to give back to the community that helped me.
Ryan Olshan
Microsoft MVP, ASP.NET
Blog | Group | Website | Strong Coders Community

How to ask a question
MikeOtown
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource2/19/2006 4:02:58 PM

0

Ok, I see what the difference is.  Since your call to PopulatePublishersGridView()
is not inside a "if (!Page.IsPostBack)", you are creating a DataTable object and Filling it from the database on every postback.  That's why you have the GridView.DataSource available in the Sorting event.  The Page_Load event is populating the GridView.DataSource before the Sorting event fires.

Thanks for posting your code.

LisaM
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource2/24/2006 2:09:58 AM

0

I can't get paging to work.  I have

OnPageIndexChanging="gvApprovals_PageIndexChanging"

in my gridview.  I assign a datatable to my gridview the the page load.  Then I run

Protected Sub gvApprovals_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvApprovals.PageIndexChanging

   gvApprovals.PageIndex = e.NewPageIndex

   gvApprovals.DataBind()

End Sub

 

Yet the darn thing won't work.  It hits that sub twice when I click next page....  and i just get a blank.  any thoughts?


~ Lisa
StrongTypes
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource2/24/2006 2:54:17 AM

0

Hi Lisa,

I'll publish a VB.NET version of the live working C# code sometime tonight.

Ryan


Ryan Olshan
Microsoft MVP, ASP.NET
Blog | Group | Website | Strong Coders Community

How to ask a question
StrongTypes
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource2/24/2006 3:48:43 AM

0

186 Items, 10 Pages 1 2 3 4 5 6 7 8 9 10 |< << Go >> >|


Free Download:


Web:
Sorting & Paging In GridView ? - ASP.NET Forums If you are using DataSet then this may help you. http://ryanolshan.com/articles/ c-gridview-sorting-paging-w-o-a-datasourcecontrol-datasource ...
Trying to use the FAQ " Sorting and paging in the GridView control ... http://ryanolshan.com/articles/c-gridview-sorting-paging-w-o-a-datasourcecontrol -datasource/. Regards,Vinz "Code, Beer and Music" that's my ...
[web refference]C# GridView Sorting/Paging w/o a DataSourceControl ... 2008年6月19日 ... /c-gridview-sorting-paging-woa-datasourcecontrol-datasource/C# ... on a GridView control without using a DataSourceControl DataSource ...
gridview sorting - Dot Net Search Engine swicki - powered by eurekster C# GridView Sorting/Paging w/o a DataSourceControl DataSource . ... ryanolshan. com/articles/c-gridview-sorting-paging-w-o-a-data. ... How to use sorting in GridView in ASP.Net. Add image in grid header to indicate sort direction. ...
gridview paging issue - ASP.NET Forums hi , iam using a gridview with paging ,but paging doesnot works ,it shows ... /c -gridview-sorting-paging-w-o-a-datasourcecontrol-datasource/ ...

HOW TO: Using sorting / paging on GridView w/o a DataSourceControl ... NET Forums If you are using DataSet then this may help you. http://ryanolshan. com/articles/ c-gridview-sorting-paging-w-o-a-datasourcecontrol-datasource . ...






access denied creating a file

custom wizard how-to not using a createuserwizard

arguments of matrix

developing a specialty browser proxy

documentation

datetimeformatinfo problem

integration between asp and asp.net

timer handle in c#

strange compile problem

adding/approving friends script in asp.net 2.0

fileupload control style

when will fire session end event?

help !!!!!! accessing shared folder on network

the stupid and useless span wrapper on web controls...

url is changed from "\filename.aspx" to "%2ffilename.aspx" - help !!

setting up cell height

system.reflection.ambiguousmatchexception

configurationmanager not reading connectionstring

help selectedweek on the calendar control

how can i call a button's onclick event in code?

how to go to the bottom of the page on every page load

anyone knows where the asp.net manual is?

supported by the .net compact framework.

how to create customererror page for remote view of all errors

uploading files to sql database

how to include the correct htm file in aspx depending on querystring?

server transfer vs response.redirect.

how can i read my mails from a pop3 mail server using asp.net

send password message

help a beginner...

 
Search This Site:

 
  Privacy | Contact Us
All Times Are GMT