Page view counter

HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource

Rate It (26)

Last post 07-30-2007 2:56 AM by krishanmanral. 185 replies.

Sort Posts:

  • HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource

    01-24-2006, 1:55 PM
    Locked
    • Loading...
    • StrongTypes
    • Joined on 12-13-2005, 4:21 PM
    • California
    • Posts 6,007
    • Points 30,696
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

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

    Ryan Olshan
    ASPInsider | Microsoft MVP, ASP.NET
    http://ryanolshan.com

    How to ask a question
    Filed under:
  • Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource

    02-07-2006, 3:13 AM
    Locked
    • Loading...
    • ChrisOngsuco
    • Joined on 01-31-2006, 12:29 PM
    • Philippines
    • Posts 66
    • Points 319
    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
  • Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource

    02-07-2006, 8:35 AM
    Locked
    • Loading...
    • StrongTypes
    • Joined on 12-13-2005, 4:21 PM
    • California
    • Posts 6,007
    • Points 30,696
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs
    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
    ASPInsider | Microsoft MVP, ASP.NET
    http://ryanolshan.com

    How to ask a question
  • Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource

    02-07-2006, 3:09 PM
    Locked
    • Loading...
    • StrongTypes
    • Joined on 12-13-2005, 4:21 PM
    • California
    • Posts 6,007
    • Points 30,696
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

    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
    ASPInsider | Microsoft MVP, ASP.NET
    http://ryanolshan.com

    How to ask a question
  • Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource

    02-10-2006, 2:24 PM
    Locked
    • Loading...
    • netNewBee
    • Joined on 02-09-2006, 8:49 PM
    • Posts 89
    • Points 213

    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 :-)
  • Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource

    02-10-2006, 2:35 PM
    Locked
    • Loading...
    • StrongTypes
    • Joined on 12-13-2005, 4:21 PM
    • California
    • Posts 6,007
    • Points 30,696
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

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

    Ryan Olshan
    ASPInsider | Microsoft MVP, ASP.NET
    http://ryanolshan.com

    How to ask a question
  • Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource

    02-10-2006, 2:44 PM
    Locked
    • Loading...
    • netNewBee
    • Joined on 02-09-2006, 8:49 PM
    • Posts 89
    • Points 213

    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 :-)
  • Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource

    02-14-2006, 4:42 PM
    Locked
    • Loading...
    • robbyram
    • Joined on 02-14-2006, 9:35 PM
    • Posts 2
    • Points 10

    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.

  • Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource

    02-14-2006, 11:02 PM
    Locked
    • Loading...
    • StrongTypes
    • Joined on 12-13-2005, 4:21 PM
    • California
    • Posts 6,007
    • Points 30,696
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

    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
    ASPInsider | Microsoft MVP, ASP.NET
    http://ryanolshan.com

    How to ask a question
  • Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource

    02-15-2006, 3:57 PM
    Locked
    • Loading...
    • robbyram
    • Joined on 02-14-2006, 9:35 PM
    • Posts 2
    • Points 10

    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.

  • Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource

    02-15-2006, 4:46 PM
    Locked
    • Loading...
    • StrongTypes
    • Joined on 12-13-2005, 4:21 PM
    • California
    • Posts 6,007
    • Points 30,696
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

    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
    ASPInsider | Microsoft MVP, ASP.NET
    http://ryanolshan.com

    How to ask a question
  • Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource

    02-18-2006, 4:24 PM
    Locked
    • Loading...
    • MikeOtown
    • Joined on 01-05-2005, 10:12 AM
    • Posts 33
    • Points 102

    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?

  • Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource

    02-18-2006, 5:16 PM
    Locked
    • Loading...
    • StrongTypes
    • Joined on 12-13-2005, 4:21 PM
    • California
    • Posts 6,007
    • Points 30,696
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

    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
    ASPInsider | Microsoft MVP, ASP.NET
    http://ryanolshan.com

    How to ask a question
  • Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource

    02-18-2006, 6:08 PM
    Locked
    • Loading...
    • StrongTypes
    • Joined on 12-13-2005, 4:21 PM
    • California
    • Posts 6,007
    • Points 30,696
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

    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
    ASPInsider | Microsoft MVP, ASP.NET
    http://ryanolshan.com

    How to ask a question
  • Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource

    02-19-2006, 3:04 AM
    Locked
    • Loading...
    • Blake05
    • Joined on 12-02-2005, 11:22 PM
    • Wisconsin
    • Posts 501
    • Points 2,498
    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.
Page 1 of 13 (186 items) 1 2 3 4 5 Next > ... Last ยป