• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2257
  • Last Modified:

ASP.NET Datagrid Paging - Setting the Virtual Item Count (virtualitemcount) Property

Hi I am stuck on an issue whilst using Dreamweaver and asp.net (both of which I am very new to). Basically the example query below returns around 20 records, I want to page the datagrid with 5 records per page. To do this though I need to set the virtualitemcount property. But I do not know how to do this at run time.

I have bound my datagrid in the following way (and am therefore not using a dataset)

Dim DBconn_1 as New SqlConnection("Data Source=Crystal;Initial Catalog=OPAPPSLIVE;User Id=xxxxx;Password=xxxxx;")
Dim DBsql_1 as String = "select distinct appt_date, Start_time, Staff_Name from appt_appointment"
Dim DBcmd_1 as New SqlCommand(DBsql_1, DBconn_1)
dgAppts.DataSource = DBcmd_1.ExecuteReader(CommandBehavior.CloseConnection)

then later on the datagrid definition

<asp:datagrid id="dgAppts" runat="server"
  virtualitemcount="WHAT DO I PUT IN HERE PLEASE"

Anyhelp would be very much appreciated...
...Thanks Steve
  • 3
  • 2
1 Solution
WIth AllowCustomPaging = false (default)  and AllowPagin = true you will only need to set the PageSize Property to determine how many items you want to display per page. If no value is set for PageSize the default page is 10 items per page. So this would work fine for only 20 records on average return:
AllowPaging = true
AllowCustomPaging = false
PageSize = 5

Normally this is fine, but the entire data source is always loaded into the DataGrid control this way and everytime the DataGrid control moves to a different page, the entire datasource is loaded once again.  When you don't want to load the entire datasource into the datagrid, you set AllowCustomPaging = true which enables the  VirtualItemCount property. Set the VirtualItemCount Property to the total number of items that will be loaded into the DataGrid control and set the PageSize to the number of items in the datagrid control to display per page. You will still need to handle the PageIndexChanged event though.
Here is a link to that might help:
swtileyAuthor Commented:
Hi lorelogic, and thank you for your comment and link

I tried implementint what you suggested, with setting the AllowCustomPaging to be false, but get the following error.

"AllowCustomPaging must be true and VirtualItemCount must be set for a DataGrid with ID 'dgHistoricAppts' when AllowPaging is set to true and the selected data source does not implement ICollection."

However I have no idea what the ICollection is. Will take a look on the internet later but if you have an idea then it would be appreciated if you could let me know.

many thanks
Sorry, the datareader (ExecuteReader) does implement the ICollection interface, which is used by the builit in page reading of the Datagrid. You would need to use the dataset for your datasource for the simple paging.  Here is a link showing how to use a Dataset and DataAdapter with the DataGrid Paging:

Here is a link showing how to use the DataReader with the DataGrid Paging and handling the Custom Paging:
That should read the datareader (ExecuteReader) does NOT (typo) implement the ICollection interface....
swtileyAuthor Commented:
Thanks for your comments lorelogic - all working now :-)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now