Link to home
Start Free TrialLog in
Avatar of swtiley
swtiley

asked on

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)
DBconn_1.Open()
dgAppts.DataSource = DBcmd_1.ExecuteReader(CommandBehavior.CloseConnection)
dgAppts.DataBind()    
--------------

then later on the datagrid definition

--------------
<asp:datagrid id="dgAppts" runat="server"
  AllowPaging="true"
  AllowCustomPaging="true"
  AllowSorting="False"
  AutoGenerateColumns="false"
  CellPadding="3"
  CellSpacing="0"
  PagerStyle-Mode="NumericPages"
  PageSize="5"
  ShowFooter="false"
  ShowHeader="true"
  OnPageIndexChanged="Historic_Page_Changer"
  virtualitemcount="WHAT DO I PUT IN HERE PLEASE"
----------------

Anyhelp would be very much appreciated...
...Thanks Steve
Avatar of lorelogic
lorelogic

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:
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.datagrid.virtualitemcount(VS.71).aspx
Avatar of swtiley

ASKER

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
...Steve
ASKER CERTIFIED SOLUTION
Avatar of lorelogic
lorelogic

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That should read the datareader (ExecuteReader) does NOT (typo) implement the ICollection interface....
Avatar of swtiley

ASKER

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