How does the LLBLDataSource set the paging data?

Posts   
 
    
jmeckley
User
Posts: 403
Joined: 05-Jul-2006
# Posted on: 15-Jun-2007 19:15:26   

I'm experimenting with MVP patterns and GridViews. I know with the LLBLDataSource control you can page the data. My understanding is the control works like this: * control queries database and returns just that page of data * control queries db to get the total number of records * control passes the total record count to the gridview * gridview automatically generates paging links based on the current page index and the total number of recordsI would like to mimic this functionality without using the DataSourceControl. if this is correct, how/where is the total records set? if this is not correct, how is this accomplished?

jmeckley
User
Posts: 403
Joined: 05-Jul-2006
# Posted on: 15-Jun-2007 21:14:34   

http://forums.asp.net/p/1069129/1554769.aspx I adapted the above post for my proof of concept. I have only tested selecting data, not editing data. here is what I have if anyone is interested.

using System;
using System.Collections;
using System.Web.UI.WebControls;

namespace My.Web.Utilities
{
    public class GridViewMVPAdapter : IGridViewMVPAdapter
    {
        private IEnumerable data;
        private int virtualItemCount;
        private GridView underlyingControl;

        public GridViewDomainModelControl(GridView gridView)
        {
            this.underlyingControl = gridView;
        }

        #region IGridViewMVPAdapterMembers

        public int PageIndex 
        { 
            get { return this.underlyingControl.PageIndex; }
            set 
            {
                if (value < -1)
                {
                    value = -1;
                }
                if (value > this.underlyingControl.PageCount)
                {
                    value = this.underlyingControl.PageCount;
                }
                this.underlyingControl.PageIndex = value; 
            }
        }

        public void BindTo(IEnumerable data, int totalRecords)
        {
            this.data = data;
            this.virtualItemCount = totalRecords;

            ObjectDataSource ods = new ObjectDataSource();
            ods.ObjectCreating += new ObjectDataSourceObjectEventHandler(ods_ObjectCreating);

            ods.EnablePaging = this.underlyingControl.AllowPaging; 
            ods.TypeName = "My.Web.Utilities.IVirtualCountTableAdapter"; 
            ods.SelectMethod = "GetData";
            ods.SelectCountMethod = "VirtualItemCount";
            ods.StartRowIndexParameterName = "startRow";
            ods.MaximumRowsParameterName = "maxRows";
            ods.EnableViewState = false;

            this.underlyingControl.DataSource = ods;
            this.underlyingControl.DataBind();
        }

        #endregion

        private void ods_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
        {
            e.ObjectInstance = new VirtualCountTableAdapter(this.data, this.virtualItemCount);
        }
    }

    public class VirtualCountTableAdapter : IVirtualCountTableAdapter
    {
        private IEnumerable data;
        private int virtualItemCount;

        public VirtualCountTableAdapter(IEnumerable data, int virtualItemCount)
        {
            this.data = data;
            this.virtualItemCount = virtualItemCount;
        }

        public int VirtualItemCount()
        {
            return this.virtualItemCount;
        }

        public IEnumerable GetData()
        {
            return this.data;
        }

        public IEnumerable GetData(int startRow, int maxRows)
        {
            return this.data;
        }
    }

    interface IVirtualCountTableAdapter
    {
        IEnumerable GetData(int startRow, int maxRows);
        IEnumerable GetData();
        int VirtualItemCount();
    }
}

Is this similar to how LLBLDataSource sets the page count?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39912
Joined: 17-Aug-2003
# Posted on: 15-Jun-2007 21:42:19   

Just take a peek into the LLBLGenProDataSource(2) classes sourcecode, it's on your harddisk simple_smile

E.g. check the LLBLGenProDataSourceView2 class. (the views do all the work). Then check protected override IEnumerable ExecuteSelect( DataSourceSelectArguments arguments ) This is the entry point. From there, the code will perform the select which have to be done, based on the type to fetch. For example, check private IEnumerable ExecuteSelectEntityCollection(int pageSize, int pageNumber, DataSourceSelectArguments arguments ) which shows you that you should set datasourcecontrol.TotalRowCount for the total # of rows. Then in the same cycle, you should fetch the page data.

Frans Bouma | Lead developer LLBLGen Pro