Archive for February 26, 2011

Developers, you are trying to implement custom paging for ASP.net GridView and sure you have read many posts and information about this subject.

Many posts used this by applying external pagers outside the GridView. this is over 🙂 lets use iheritance to implement that:

Create the extension class CustomPagingGridView that inherits GridView as follows:

public class CustomPagingGridView : GridView  

{
public CustomPagingGridView()
        : base()
{
this.AllowPaging = true;
this.AllowSorting = true;
this.PagerSettings.Mode = PagerButtons.NumericFirstLast;
}

#region Custom properties

[Browsable(true), Category(“Updated”)]
[Description(“Set the Database table total item count for this grid”)]
public int TotalDatabaseItemCount
{
get
{
if (ViewState[“pgv_vitemcount”] == null)
ViewState[“pgv_vitemcount”] = -1;
return Convert.ToInt32(ViewState[“pgv_vitemcount”]);
}
set
{
ViewState[“pgv_vitemcount”] = value;
}
}

[Browsable(true), Category(“Updated”)]
[Description(“Get the item ordering when sorting event is triggered”)]
public string OrderBy
{
get
{
if (ViewState[“pgv_orderby”] == null)
ViewState[“pgv_orderby”] = string.Empty;
return ViewState[“pgv_orderby”].ToString();
}

protected set
{
ViewState[“pgv_orderby”] = value;
}
}

private int CurrentPageIndex
{
get
{
if (ViewState[“pgv_pageindex”] == null)
ViewState[“pgv_pageindex”] = 0;
return Convert.ToInt32(ViewState[“pgv_pageindex”]);
}
set
{
ViewState[“pgv_pageindex”] = value;
}
}

private bool CustomPaging
{
get
{
return (TotalDatabaseItemCount != -1);
}
}
#endregion

#region Overriding the parent methods

public override object DataSource
{
get
{
return base.DataSource;
}
set
{
base.DataSource = value;
CurrentPageIndex = PageIndex;
}
}

protected override void OnSorting(GridViewSortEventArgs e)
{
SortDirection direction = SortDirection.Ascending;
if (ViewState[e.SortExpression] != null && (SortDirection)ViewState[e.SortExpression] == SortDirection.Ascending)
{
direction = SortDirection.Descending;
}
ViewState[e.SortExpression] = direction;
OrderBy = string.Format(“{0} {1}”, e.SortExpression, (direction == SortDirection.Descending ? “DESC” : “”));
base.OnSorting(e);
}

protected override void InitializePager(GridViewRow row, int columnSpan, PagedDataSource pagedDataSource)
{
if (CustomPaging)
{
pagedDataSource.AllowCustomPaging = true;
pagedDataSource.VirtualCount = TotalDatabaseItemCount;
pagedDataSource.CurrentPageIndex = CurrentPageIndex;
}
base.InitializePager(row, columnSpan, pagedDataSource);
}

#endregion

}

When you include this class within your code and compile, a “CustomPagingGridView” will be added in the toolbar under the pointer icon while opening any aspx file

Add it to your design view and set its properties as desired.

Properties:

when clicking on the newly added custom gridview in the properties part, a new section named “Updated” is added containing two properties that can be programmatically set, these properties are “TotalDatabaseItemCount” and “OrderBy”

The total database item count is the total number to be fetched for example if you want to fetch 1000 item from database 10 by 10 (1000 is the count to be fetched ex “select Count(*) from table1″)

Next you have to set the TotalDatabaseItemCount=1000 and get records from database 10 by 10 using inline stored procedure or inner query “select * from table1 where rownum>10 && rownum<20” this will fetch the first bulk.

In the previous example we are getting the second page of the gridview if we has a pager of 10, note that the number can be populated from the current index and page number.

Hope this will be useful. Feel free to ask any question or inquire about any example.