Auto Ad Code

Tuesday, March 5, 2013

Datagrid/GridView Paging not working if set(initialize) from codebehind

Okay, here is another frustrating issue which you may face while using Datagrid or Gridview and you want to set (initialize) paging from code behind.
Your code will looks perfect to you but still paging is not functional.
Your code may look like
C#

  gvUsers.DataSource = mDtUsers; //mDtUsers is your datasource.
  gvUsers.DataBind();

  gvUsers.AllowPaging = true;
  gvUsers.PageSize = mPageSize;
  gvUsers.CurrentPageIndex = 0;


VB.Net

  gvUsers.DataSource = mDtUsers 'mDtUsers is your datasource.
  gvUsers.DataBind()

  gvUsers.AllowPaging = True
  gvUsers.PageSize = mPageSize
  gvUsers.CurrentPageIndex = 0


Everything looks fine? Yeh it do but still paging not functional. So whats the solution?

The Solution

Solution is pretty easy and straight forward and in fact you don't have to add any extra line of code, you simply have to change the sequence of statements. That means set (initialize) the paging before the binding of Datagrid/Gridview and everything will be working perfect, it means your code should be like

C#

  gvUsers.AllowPaging = true;
  gvUsers.PageSize = mPageSize;
  gvUsers.CurrentPageIndex = 0;

  gvUsers.DataSource = mDtUsers; //mDtUsers is your datasource.
  gvUsers.DataBind();



VB.Net

  gvUsers.AllowPaging = True
  gvUsers.PageSize = mPageSize
  gvUsers.CurrentPageIndex = 0

  gvUsers.DataSource = mDtUsers 'mDtUsers is your datasource.
  gvUsers.DataBind()


Hope this simnply solution will solve your problem.
Happy Coding! :)


No comments:

Post a Comment