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


About the Author

Nikolay Diyanov

Diyanov is the Product Manager of the Native Mobile UI division at Progress. Delivering outstanding solutions that make developers' lives easier is his passion and the biggest reward in his work. In his spare time, Nikolay enjoys travelling around the world, hiking, sun-bathing and kite-surfing.

Find him on Twitter @n_diyanov or on LinkedIn.

Comments

Comments are disabled in preview mode.