VirtualItemCount in datagrid.

  • Thread starter Thread starter archana
  • Start date Start date
A

archana

Hi all,

Can anyone tell me what exactly VirtualItemCount in datagrid. I read
on msdn but not clear about it.

Any help will be truely appreciared.

thanks in advance
 
Hi there,

Imagine you want to display a pager below a datagrid in order to allow users
to navigate through data query results returned from database. In 99%
applications results are displayed in paged form, meaning user is presented
with a small chunk of results, i.e. search result returned 1350 records, but
due to several reasons (perfomance, user experience, etc.) your list shows
only 10 records at time. I believe you know all that. DataGrid has got built
in mechanism for paging which is based on entire result set assigned to
datagrid's datasource property. It requires all the records to be included in
paging, which means if you wanted to display only 10 records in 10th page
from your result set (records from 91 to 100) you would have to fetch entire
result set (1350 rows) locally (transfer all the records from database to
local's machine memory). As you can guess it is very inefficient. Therefore,
developers invented much better techniques (custom paging) allowing fetching
required information only . Most of them require page number (or start row
index) and page size (or end row index), but please note to display pager
properly (i.e. show/hide next page button) you have to know how many rows
(pages) are within result set (i.e. query returned 1350 rows, current page =
2, you should show previous and next buttons because you know there are
records before and after as number of matching records is 1350). And here
comes virtualitemcount property which holds total number of rows matching
criteria, which in this case would be 1350. I know it might sound difficult,
but it's easire to understand it on real life example presented by one of
this asp.net community guys Peter Bromber:

http://www.eggheadcafe.com/articles/20060109.asp

Hope this helps
 
Hi,

thanks for your reply.

but still i have one confusion regarding virtualitemcount. Does
navigation bar displayed at the bottom of datagrid showing page number
depends on virtualitemcount?

Does it means i need to set virtualitemcount to total number of
records which present in table which i am displaying page by page into
datagrid?

if yes then does it means that every time going from one page to
another i need to fire query into database passing current page index
and retrieving those many records from database according to page
size?

Please correct me if i am wrong.

thanks in advance.
 
Hi there Archana,


Howdy. Seems you got it right but you're missing relationship between all
the variables (especially virtualitemcount - pagecount). Let's analyse a real
life example.

PageSize = 10 (number of records being displayed in datagrid control at once)
VirtualItemCount = 35 (number of records matching criteria)

Case 1. First page is selected (CurrentPageIndex = 0)
Total number of pages:
PageCount = (VirtualItemCount+PageSize - 1) / PageSize = (35 + 10 - 1) / 10
= 4
so the data grid should look like this:
1. | blabla |
2. | blabla |
3. | blabla |
.....
10. | blabla |
[1] 2 3 4 >

as you can see, virtualitemcount = 35 so you may display buttons pages
2,3,4. Because this is the first page you cannot move to previous page (<
button is invisible)

Case 2. Now we moved to second page.

11. | blabla |
12. | blabla |
13. | blabla |
.....
20. | blabla |
< 1 [2] 3 4 >
There are 4 pages, 2nd is currently selected, we can display prev/next
buttons as there are records before and after.

Case 3. And finally we selected last page
31. | blabla |
32. | blabla |
33. | blabla |
34. | blabla |
35. | blabla |
< 1 2 3 [4]
VirtualItemCount literally represents the last item index.
if yes then does it means that every time going from one page to
another i need to fire query into database passing current page index
and retrieving those many records from database according to page
size?

Yes you have to go to database to get result for currently selected page. In
our examples you would have to run queries to get records with sequence
number within result set
Case 1) from 1 to 10
Case 2) from 11 to 20
Case 3) from 31 to 40 but due to the fact there are only five records on
this page you would get records from 31 to 35.

It it clear now?

Best regards

Milosz
 
Back
Top