X

GridView and Invalid CurrentPageIndex Value Exception – .Net 3.5 Version

In my previous GridView entry titled as GridView and Invalid CurrentPageIndex Value Exception I made an example about how to corrigate paged GridView page index before grid is bound to data. Let’s to id now .Net Framework 3.5 way.

public static class GridViewExtensions
{
    public static void CorrigatePageIndex(this GridView gdv, Int32 rowCount)
    {
        Int32 newIndex = gdv.PageIndex;
        if (gdv.PageSize > 0)
            if ((Int32)Math.Ceiling((decimal)rowCount / gdv.PageSize) < newIndex)
                newIndex = (Int32)Math.Ceiling((decimal)rowCount / gdv.PageSize);
        if (newIndex < 0)
            newIndex = 0;
        gdv.PageIndex = newIndex;
    }
}

Now we can corrigate page index usng the following code.

protected void Page_Load(object sender, EventArgs e)
{
    if (this.IsPostBack)
        return;
    DataTable dt = GetRows();
    gdvList.PageIndex = 15;
    gdvList.DataSource = dt;
    gdvList.CorrectCurrentPage(dt.Rows.Count);
    gdvList.DataBind();
}
Liked this post? Empower your friends by sharing it!
Categories: ASP.NET
Related Post