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:
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:
<organization id="1" statename="Alaska" stateabbr="AK" />
And a corresponding entity will look like:
[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:
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
- Place
TestWebService
source in some folder, for example C:\TestWebService - Place
TestWinForm
source in C:\TestWinForm - Run IIS
- At default web site, create
TestWebService
virtual directory so that its source folder would be C:\TestWebService folder - Run
TestWinForm
app
Links
History
- 17th April, 2009: Initial post