Click here to Skip to main content
15,867,832 members
Articles / Desktop Programming / Windows Forms

Data Pagination in DataGridView. Bounded Mode

Rate me:
Please Sign up or sign in to vote.
4.67/5 (9 votes)
17 Apr 2009CPOL2 min read 64.1K   2.2K   38   8
How to use DataGridView pagination in bounded mode

Introduction

Most of the time, I use .NET Framework 2.0 and its Windows Forms API. One of the most widely used controls is a DataGridView. But as it has already been said many times it leaks of data paging. There are a lot of articles all over the Internet dealing with paging in a DataGridView. Most of them propose a use of DataGridView virtual mode. I will try to implement it in a bounded mode.

Background

I was working with web-services that suppose a lot of work with XML data and once had to implement embedded .NET control for Internet Explorer that was supposed to show to a user large lists of data in DataGridView controls. Moreover, users should be able to filter and sort the data shown. There were no problems with lists that had 100-300 rows. But if a list had something like 10000 - 20000 rows, then it is another situation. Using of the DataGridView virtual mode did not resolve a problem with filtering and sorting, so one of the solutions was to implement caching in the bounded mode.

Using the Code

The code is just an idea. So you should implement your own data access layer. It means web-services or access to a local database. You could implement a functionality of IBindedListView interface that I missed. I've used XML request as an argument of page provider but you can implement your own realization that will meet your needs.

Underlying Layer

At this level, we will define an entity to be shown in our DataGridView. Let’s choose a company list with the following structure:

C#
Create table Organizations 
(
ID int,
StateAbbr char(2),
StateName char(50),
OrganizationName char(150),
AgencyName char(150),
LocalPhone char(20)
);		

A corresponding XML element will look like:

XML
<organization id="1" statename="Alaska" stateabbr="AK" />

And a corresponding entity will look like:

C#
[XmlRoot(ElementName="Organization")]
    public class Organization
    {
        private int mID;
        private string mStateAbbr = String.Empty;
	  …

        [XmlAttribute("ID")]
        public int ID
        {
            get { return mID; }
            set { mID = value; }
        }

        [XmlAttribute("StateAbbr")]
        public string StateAbbr
        {
            get { return mStateAbbr; }
            set { mStateAbbr = value; }
        }
	  …
    } 		

So we could deserialize it.

Page Provider

We should design a base interface to determine how page provider will retrieve the corresponding page and also filter and sort data:

C#
public interface IPageProvider<t>
{
    List<t> GetDataPage(int pageNumber, int rowsPerPage);
    string Filter { get; set;}
    string Sort { get; set;}
    int RowCount { get;}
}

Cache

The cache class will be analogous to the one in MSDN article with the only difference that List<> class is used vs. DataTable class.

CachedBindingListView

To use our cache as data source for the DataGridView, we should implement IBindingListView interface.

Server Side

At server side, web service will retrieve specified page of data and apply filtering and sorting. To demonstrate it, I've used Microsoft Access database and constructed SQL request to it.

How to Use the Code

  1. Place TestWebService source in some folder, for example C:\TestWebService 
  2. Place TestWinForm source in C:\TestWinForm 
  3. Run IIS 
  4. At default web site, create TestWebService virtual directory so that its source folder would be C:\TestWebService folder 
  5. Run TestWinForm app

Links

History

  • 17th April, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Russian Federation Russian Federation
• More than 10 years experience in software development
• 3 years experience in direction of automation department.
• software engineering: experience in the whole life cycle of software development
• languages: Kotlin, Swift,Objective-C, C++, C#, Java, ASP.NET, HTML, XML, JavaScript, Visual FoxPro, MS SQL, T-SQL
• Gathering, specification and the analysis of requirements of the customer to the software.
• The analysis of a subject area.
• Estimations of labour input of development.
And so on...

Comments and Discussions

 
GeneralConcerns about performance with large records numbers Pin
alan brett powell17-Jun-10 3:26
alan brett powell17-Jun-10 3:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.