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(Comm andBehavio r.CloseCon nection)
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="NumericPa ges"
PageSize="5"
ShowFooter="false"
ShowHeader="true"
OnPageIndexChanged="Histor ic_Page_Ch anger"
virtualitemcount="WHAT DO I PUT IN HERE PLEASE"
----------------
Anyhelp would be very much appreciated...
...Thanks Steve
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(Comm
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="NumericPa
PageSize="5"
ShowFooter="false"
ShowHeader="true"
OnPageIndexChanged="Histor
virtualitemcount="WHAT DO I PUT IN HERE PLEASE"
----------------
Anyhelp would be very much appreciated...
...Thanks Steve
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
That should read the datareader (ExecuteReader) does NOT (typo) implement the ICollection interface....
ASKER
Thanks for your comments lorelogic - all working now :-)
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