Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Downloads | Blogs | E-Books | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » Custom Controls » GridView with DataPager in ASP.NET 3.5

GridView with DataPager in ASP.NET 3.5

To page through data in a control that implements the IPageableItemContainer interface, DataPager control can be used. GridView has its own paging and does not implement IPageableItemContainer interface.

Author Rank:
Technologies: ASP.NET 3.5, Controls,Visual C# .NET
Total downloads : 1355
Total page views :  34862
Rating :
 3/5
This article has been rated :  1 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
DataPagerGridView.zip
 
Become a Sponsor


Related EbooksTop Videos

To page through data in a control that implements the IPageableItemContainer interface, DataPager control can be used. GridView has its own paging and does not implement IPageableItemContainer interface. ListView is the only control that works with DataPager.

The DataPager control supports built-in paging user interface (UI). NumericPagerField object enables users to select a page of data by page number. NextPreviousPagerField object enables users to move through pages of data one page at a time, or to jump to the first or last page of data. The size of the pages of data is set by using the PageSize property of the DataPager control. One or more pager field objects can be used in a single DataPager control. Custom paging UI can be created by using the TemplatePagerField object. In the TemplatePagerField template, the DataPager control is referenced by using the Container property which provides access to the properties of the DataPager control. These properties include the starting row index, the page size, and the total number of rows currently bound to the control.

So let's begin extending the GridView with the IPageableItemContainer interface.

Define a new class as below
/// <summary>

/// DataPagerGridView is a custom control that implements GrieView and IPageableItemContainer

/// </summary>

public class DataPagerGridView : GridView, IPageableItemContainer

{
}

MaximumRows will be equal to the PageSize property
/// <summary>

/// IPageableItemContainer's MaximumRows  = PageSize property

/// </summary>

int IPageableItemContainer.MaximumRows

{

    get { return this.PageSize; }

}

StartRowIndex can be calculated from the PageSize and PageIndex properties
/// <summary>

/// IPageableItemContainer's StartRowIndex = PageSize * PageIndex properties

/// </summary>

int IPageableItemContainer.StartRowIndex

{

    get { return this.PageSize * this.PageIndex; }

}

The SetPageProperties method is called by the DataPager control every time that the control must update the page-related properties. When implemented by a class, the SetPageProperties method must internally update the MaximumRows and StartRowIndex properties by using the values of the maximumRows and startRowIndex parameters. So set the Grid with appropriate parameters and bind to right chunk of data.
/// <summary>

/// Set the control with appropriate parameters and bind to right chunk of data.

/// </summary>

/// <param name="startRowIndex"></param>

/// <param name="maximumRows"></param>

/// <param name="databind"></param>

void IPageableItemContainer.SetPageProperties(int startRowIndex, int maximumRows, bool databind)

{

    int newPageIndex = (startRowIndex / maximumRows);

    this.PageSize = maximumRows;

    if (this.PageIndex != newPageIndex)

    {

        bool isCanceled = false;

        if (databind)

        {

            //  create the event arguments and raise the event

            GridViewPageEventArgs args = new GridViewPageEventArgs(newPageIndex);

            this.OnPageIndexChanging(args);

            isCanceled = args.Cancel;

            newPageIndex = args.NewPageIndex;

        }

        //  if the event wasn't cancelled change the paging values

        if (!isCanceled)

        {

            this.PageIndex = newPageIndex;

            if (databind)

                this.OnPageIndexChanged(EventArgs.Empty);

        }

        if (databind)

            this.RequiresDataBinding = true;

    }

}

For the DataPager to render the correct number of page buttons and enable/disable them, it needs to know the total number of rows and the Page Size. But this information is with the GridView and not known to the DataPager.As the GridView control inherits from CompositeDataboundControl which contains a variant of the CreateChildControls method that also takes in a property indicating whether the control is being bound to data or simply re-rendered. The GridView uses this method to bind to its data source and here we can place a trigger for the TotalRowCountAvailable event to be raised. Call base control's CreateChildControls method and determine the number of rows in the source then fire off the event with the derived data and then we return the original result.
protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding)

{

    int rows = base.CreateChildControls(dataSource, dataBinding);

    //  if the paging feature is enabled, determine the total number of rows in the datasource

    if (this.AllowPaging)

    {

    //  if we are databinding, use the number of rows that were created, otherwise cast the datasource to an Collection and use that as the count

    int totalRowCount = dataBinding ? rows : ((ICollection)dataSource).Count;

    //  raise the row count available event

    IPageableItemContainer pageableItemContainer = this as IPageableItemContainer;

    this.OnTotalRowCountAvailable(new PageEventArgs(pageableItemContainer.StartRowIndex,pageableItemContainer.MaximumRows,totalRowCount));

    //  make sure the top and bottom pager rows are not visible

    if (this.TopPagerRow != null)

        this.TopPagerRow.Visible = false;

    if (this.BottomPagerRow != null)

        this.BottomPagerRow.Visible = false;

    }

    return rows
}

 That's all you are done, put the control on aspx page and use DataPager with GridView Contol.

 

 


Login to add your contents and source code to this article
 [Top] Rate this article
 About the author
 
Nipun Tomar
Nipun has 5 years working experience in .NET technologies. He holds Bachelor's and Master's degree in Computer Science. Currently working on ASP.NET 2.0/3.5, VB.NET, C#.NET, AJAX, SQL Server 2005, WPF, WCF and Silverlight.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010
Microsoft Visual Studio 2010 offers more to developers than any other Visual Studio release. Work more productively and collaboratively-with greater control over your work at every step. The Beta 2 can give you a head start on achieving efficiency.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
DataPagerGridView.zip
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Dundas Dashboard
Become a Sponsor
 Comments
What is MCN Controls by kojikabutosv On August 25, 2008
Hi, good article, only i have a questions, the MCN Controls are of the thirds. Regards
Reply | Email | Delete | Modify | 
Problem, help me please by Ibrahim On December 1, 2008
Hello, I added your work to my project but I have an error at this point GridViewPageEventArgs args = new GridViewPageEventArgs(newPageIndex); this.OnPageIndexChanging(args); " An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code Additional information: The GridView 'DataPagerGridView1' fired event PageIndexChanging which wasn't handled. "
Reply | Email | Delete | Modify | 
Why use a dataPager with gridview? by fedrok On December 19, 2008
Hi. I'm sorry but I'm still newbie in .NET. So I can't understand why use a dataPager with gridview. What does dataPager offer more than built-in paging in gridview? Thx!
Reply | Email | Delete | Modify | 
Problem PageIndexChanging which wasn't handled. by Lisseth On April 8, 2009
I have a problem in this line: GridViewPageEventArgs args = new GridViewPageEventArgs(newPageIndex); this.OnPageIndexChanging(args); is the same problem that Ibrahim : An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code Additional information: The GridView 'DataPagerGridView1' fired event PageIndexChanging which wasn't handled. " Please help me.
Reply | Email | Delete | Modify | 
Re: Problem PageIndexChanging which wasn't handled. by Nipun On April 9, 2009
I was unable to reproduce the error, sample code is working fine. This error generally occus when gridview's OnSelectedIndexChanging is set but not defined in codebehind.

 

Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 1999 - 2009  Mindcracker LLC. All Rights Reserved