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!

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

    Leave a Reply

    Your email address will not be published. Required fields are marked *