title: Enable Paging with Dynamically/Programmatically created Grid View in C# .ASP
body: <p>I have a problem in enabling paging in a dynamically created gridview in C# .ASP 2010.</p>
  
  <pre><code>  protected void ButtonViewFiles_Click(object sender, EventArgs e)
    {
       ClearPanelConfigure();
  
          /* 
          logic to populate variable "gridviewlist" which is a type of List&lt;string&gt;
          ...
  
          */
  
       gv = new GridView();
       gv.ShowHeader = false;
       gv.BorderWidth = 0;
       gv.ForeColor = CONTROL_FONT_COLOR;
       gv.PageSize = 10;
       gv.RowDataBound += new GridViewRowEventHandler(GridView_OnRowDataBound);
       gv.AllowPaging = true;
       gv.PageIndexChanging += new GridViewPageEventHandler(GridView_PageIndexChanging);
  
       if (gridviewlist.Count &gt; 0)
       {
          gv.DataSource = gridviewlist;
          gv.DataBind();
          PanelConfigure.Controls.Add(gv);
          mySession.SaveSessionObject(gridviewlist, "GridViewList");
       }
    }
  
  
    protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {        
       gv.PageIndex = e.NewPageIndex;
       gv.DataBind();
    }
  
  
    // Enable HTML decode for each cell in the gridview rows.
    protected void GridView_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
       if (e.Row.RowType == DataControlRowType.DataRow)
       {
          for (int c = 0; c &lt; e.Row.Cells.Count; c++)
          {
             e.Row.Cells[c].Text = Server.HtmlDecode(e.Row.Cells[c].Text);
          }
       }
    }
  </code></pre>
  
  <p>Unlike most people drag and drop the GridView control at design time, the Gridview in my case has to be programmatically created at the run time. That is why I have "gv = new GridView();" In other words, if user does not click a button and fires the button event, I will not show the GridView with all the URL links to files.</p>
  
  <p>Once "gv" is created and enabled with paging, gv is added to PanelConfigure (which is type of Panel). Then user will be able to view all the file links.</p>
  
  <p>"gv" is a global variable, otherwise "gv" will not compile in </p>
  
  <pre><code>  protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {        
       gv.PageIndex = e.NewPageIndex;
       gv.DataBind();
    }
  </code></pre>
  
  <p>For people with drag and drop GridView at design time, this is not a problem. Because "gv" is defined with global scope. </p>
  
  <p>If I don't enable paging in "gv". The web application at least runs and shows all the files in one shot. However, if I enable paging at run time with "gv.AllowPaging = true;", exception is thrown which points to "gv.DataBind();" in button event handler function </p>
  
  <pre><code>  protected void ButtonViewFiles_Click(object sender, EventArgs e)
    {        
       ...
  
       gv.DataBind();
  
       ...
    }
  </code></pre>
  
  <p>The exception says: NullReferenceException was unhandled by user code. Object reference not set to an instance of an object.</p>
  
  <p>System.NullReferenceException was unhandled by user code</p>
  
  <pre><code>  HResult=-2147467261
    Message=Object reference not set to an instance of an object.
    Source=System.Web
    StackTrace:
         at System.Web.UI.WebControls.GridView.get_StateFormatter()
         at System.Web.UI.WebControls.GridView.BuildCallbackArgument(Int32 pageIndex)
         at System.Web.UI.WebControls.GridView.CreateNumericPager(TableRow row, PagedDataSource pagedDataSource, Boolean addFirstLastPageButtons)
         at System.Web.UI.WebControls.GridView.InitializePager(GridViewRow row, Int32 columnSpan, PagedDataSource pagedDataSource)
         at System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource)
         at System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding)
         at System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data)
         at System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data)
         at System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data)
         at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback)
         at System.Web.UI.WebControls.DataBoundControl.PerformSelect()
         at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
         at System.Web.UI.WebControls.GridView.DataBind()
         at DataSearcher.advanced.ButtonViewFiles_Click(Object sender, EventArgs e) in C:\Users\fgui\documents\visual studio 2010\Projects\DataSearcher\DataSearcher\Advanced.aspx.cs:line 1853
         at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
         at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
         at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
         at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
         at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
         at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
    InnerException: 
  </code></pre>
  
  <p>I searched this web site. </p>
  
  <p><a href="https://stackoverflow.com/questions/13526755/how-to-create-paging-in-a-dynamic-gridview">how to create paging in a dynamic gridview</a></p>
  
  <p>The above post does not provide an answer to dynamically created GridView. It only works for GridView drag and drop at design time. I am not quite familiar with GridView. So any help is appreciated.</p>
  
  <p>Thanks.</p>