[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2111
  • 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)
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
0
swtiley
Asked:
swtiley
  • 3
  • 2
1 Solution
 
lorelogicCommented:
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
0
 
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
...Steve
0
 
lorelogicCommented:
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:
http://www.samspublishing.com/articles/article.asp?p=32056

Here is a link showing how to use the DataReader with the DataGrid Paging and handling the Custom Paging:
http://www.samspublishing.com/articles/article.asp?p=32056&seqNum=3
0
 
lorelogicCommented:
That should read the datareader (ExecuteReader) does NOT (typo) implement the ICollection interface....
0
 
swtileyAuthor Commented:
Thanks for your comments lorelogic - all working now :-)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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