|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionThe I think a neat solution to the custom pagination is to allow the setting of the VirtualItemCount property just like in This article intentionally focuses on the GridView paging display and interaction aspects and not on the retrieving of the data block. However to be complete I have included example of retrieving page block of rows from SQL Server 2005 using the Using the codeIf you include the PagingGridView in your ASP.NET Web Application project or a class library that is referenced by your ASP.NET Web Site project, the PagingGridView component will appear in your toolbox. To add it to your page, you can just drag and drop it the same way you would use any other web controls. Or if you wish to add in the code manually to the ASCX/ASPX file, first you have to register the tag in the beginning of the file and include the PagingGridView control element qualified by the TagPrefix. See the example below: <%@ Register Assembly="PagingGridView" Namespace="Fadrian.Web.Control" TagPrefix="cc1" %> ... <cc1:PagingGridView ID="PagingGridView2" runat="server"/> PagingGridView Code ExplainationAchieving the paging functionality described above is actually quite simple The key to the implementation lies in the protected override void InitializePager(GridViewRow row, int columnSpan, PagedDataSource pagedDataSource)
public int VirtualItemCount { get { if (ViewState["pgv_vitemcount"] == null) ViewState["pgv_vitemcount"] = -1; return Convert.ToInt32(ViewState["pgv_vitemcount"]); } set { ViewState["pgv_vitemcount"] = value; } } private bool CustomPaging { get { return (VirtualItemCount != -1); } } There is an internal property private int CurrentPageIndex { get { if (ViewState["pgv_pageindex"] == null) ViewState["pgv_pageindex"] = 0; return Convert.ToInt32(ViewState["pgv_pageindex"]); } set { ViewState["pgv_pageindex"] = value; } } public override object DataSource { get { return base.DataSource; } set { base.DataSource = value; // we store the page index here so we dont lost it in databind CurrentPageIndex = PageIndex; } } Data Source and Paged DataThis article is not intended to go into the detail on how to retrieve the paged data from database; instead it provides the information here for completeness of demonstrating the use of the To support custom paging, we really need at least two things: the total number of records that we want to display (we set this to the The For more information on using private const string demoConnString = @"Integrated Security=SSPI;Persist Security Info=False;" + @"Initial Catalog=NorthwindSQL;Data Source=localhost\SQLEXPRESS"; private const string demoTableName = "Customers"; private const string demoTableDefaultOrderBy = "CustomerID"; private int GetRowCount() { using (SqlConnection conn = new SqlConnection(demoConnString)) { conn.Open(); SqlCommand comm = new SqlCommand(@"SELECT COUNT(*) FROM " + demoTableName, conn); int count = Convert.ToInt32(comm.ExecuteScalar()); conn.Close(); return count; } } private DataTable GetDataPage(int pageIndex, int pageSize, string sortExpression) { using (SqlConnection conn = new SqlConnection(demoConnString)) { // We always need a default sort field for ROW_NUMBER() to work correctly if (sortExpression.Trim().Length == 0) sortExpression = demoTableDefaultOrderBy; conn.Open(); string commandText = string.Format( "SELECT * FROM (select TOP {0} ROW_NUMBER() OVER (ORDER BY {1}) as ROW_NUM, * " +"FROM {2} ORDER BY ROW_NUM) innerSelect WHERE ROW_NUM > {3}", ((pageIndex + 1) * pageSize), sortExpression, demoTableName, (pageIndex * pageSize)); SqlDataAdapter adapter = new SqlDataAdapter(commandText, conn); DataTable dt = new DataTable(); adapter.Fill(dt); conn.Close(); dt.Columns.Remove("ROW_NUM"); return dt; } } In case you are wondering where the sample data for this article comes from, I created the NorthwindSQL database by opening the Northwind Access database and used the Upsizing wizard to create a new database (complete schema with data) in my SQL Server 2005 Express. You can repeat this process to recreate the data to test the code, or otherwise simply modify the In ActionTo allow custom paging in your target page, you will have to set the Syntactically, we code the protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { PagingGridView1.VirtualItemCount = GetRowCount(); BindPagingGrid(); } } protected void PagingGridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { PagingGridView1.PageIndex = e.NewPageIndex; BindPagingGrid(); } private void BindPagingGrid() { PagingGridView1.DataSource = GetDataPage(PagingGridView1.PageIndex, PagingGridView1.PageSize, PagingGridView1.OrderBy); PagingGridView1.DataBind(); }
|
||||||||||||||||||||||