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

Posted on 2007-07-28
Last Modified: 2010-08-05
Hi I am stuck on an issue whilst using Dreamweaver and (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
Question by:swtiley
    LVL 3

    Expert Comment

    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:
    LVL 1

    Author Comment

    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
    LVL 3

    Accepted Solution

    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:
    LVL 3

    Expert Comment

    That should read the datareader (ExecuteReader) does NOT (typo) implement the ICollection interface....
    LVL 1

    Author Comment

    Thanks for your comments lorelogic - all working now :-)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now