Emulating Paging with RadGridView for WinForms and LINQ with 1 million records

Tuesday, September 02, 2008 by pavlov | Comments 2

Our customers who use the Telerik ASP.NET line of controls are used to having paging in their grids. This approach, however, is seldom used for WinForms applications, although there are cases when paging in WinForms grids is needed as well, especially when there are million records in the database (literally).

You may have seen the blog post by Vladimir Enchev about implementing paging with RadGridView for WPF (if you have not, this is the right time to do so :)). A couple of days ago Mr. Enchev came by and said "You know guys, you could do the same thing with your WinForms grid control." And I decided to do so.

Paging emulation with RadGridView for WinForms is possible, especially if we take full advantage of LINQ. This approach also gives us another advantage - client performance. The data processing (filtering, sorting and paging) is obviously done by the SQL server, which is fully optimized for such things, rather than the application. The client only processes and shows one page at a time, rather than all million records. Here I do not use RadGridView’s Virtual Mode - this is a topic for a more advanced blog post.

I have tried to keep the application as simple as possible in order to highlight the code that implements the actual paging. Here is a screenshot of the application:

RadGridView_With_Paging_Vista

 

The important part of the code is the BindGrid method that does the actual loading and filtering of the data:

 

private void BindGrid()
{
this.radGridView1.GridElement.BeginUpdate();

IQueryable queryable = new DataClasses1DataContext().MyTables.AsQueryable();

if (!String.IsNullOrEmpty(where))
{
queryable = queryable.Where(where);
}

if (!String.IsNullOrEmpty(orderBy))
{
queryable = queryable.OrderBy(orderBy);
}

radGridView1.DataSource = queryable.Skip(currentPageIndex * pageSize).Take(pageSize);

this.radGridView1.GridElement.EndUpdate(true);

EnableDisablePager();
}

We are using the dynamic LINQ extensions for the sorting and filtering functionality. And yes - you guessed correctly - it is called when the form loads. It uses some class fields that are set from the event handlers in the application. For example when the user clicks the filter button the following method is called:

private void Filter(object sender, EventArgs e)
{
string text = FilterTextBox.Text;
where = (text == "") ? "" : String.Format(@"Name.Contains(""{0}"")", text);

BindGrid();
}

The sample also supports sorting by handling the SortChanging event of RadGridView. You can find the full source code in the attached project (the required Telerik assemblies are included in the bin/Debug folder).

Would you like to see more blog posts on using RadGridView with LINQ? Just leave a comment with your requirements and I will try to create it for you :)

WindowsFormsApplication1.zip

 

2 Comments

  • Michael Vederman 10 May 2012
    I notice that you set the currentPageIndex = 0 when sorting on a columnis requested. In my experience, users do not want the view reset to thefirst page when sorting.
    I have not tried your solution as presented, but paging in grid usingthe Telerik version we are 'stuck' with for the moment (does not havebuilt-in Winforms paging grid) is a real problem for me. The datasetpresented is sorted for the page to view, but the control resorts thealready sorted data and I get an inverted view of the sorted page...
    So, I'm wondering if you avoid dealing with this by resetting the pageto zero. If so, well, I need to keep looking for a solution... If not, then I need to give this a look.
    Thanks
  • WinForms Team 11 May 2012
    There is a better example which demonstrates how to implement paging with RadGridView in our code library:
    http://www.telerik.com/community/code-library/winforms/gridview/gridview-paging-using-commandbar.aspx 
    If you have any further questions, please open a support ticket and we will be glad to help.

Add comment

  1. Formatting options
       
     
     
     
     
       
  2. (optional, emails won't be shown on public pages)
  3. (optional)