GridView Pagination issue when dynamically added….!

Posted on Updated on

Hello ASP.NET Developers… Today I came across a small issue which killed large amount of my development time. The issue was a little tricky between the lines of code. I have an ASP.NET gridview which is dynamically (on the fly) created on my page. When I am inserting 2 lines of code  just for allowing the gridview pagination, code breaks at runtime giving  a NullReferenceException .

Cause of the issue :  i was adding the dynamically created  gridview(object) into page’s control collection after the databinding. This works fine if there is no pagination code added. But if there is code which allows pagination for the dynamically added gridview, this gridview should be added to the page’s control collection before the databinding.

Please see the below mentioned code blocks for the difference.

This piece of code will give the Null Reference Exception:

GridView gvDepartment = new GridView ();
gvDepartment.ID = "gvDepartment";

gvDepartment.AllowPaging = true;
gvDepartment.PageSize = 4;

gvDepartment.DataSource = GetDepartmentRows ();
gvDepartment.DataBind ();

this.form1.Controls.Add ( gvDepartment );

This is the perfect code:

        GridView gvDepartment = new GridView ();
        gvDepartment.ID = "gvDepartment";

        gvDepartment.AllowPaging = true;
        gvDepartment.PageSize = 4;

	/* Need to add gridview into controls collection before databind,if there is pagination */
        this.form1.Controls.Add ( gvDepartment );

        gvDepartment.DataSource = GetDepartmentRows ();
        gvDepartment.DataBind ();

NB: Obviously PageIndexChanging Event handling is needed. But this post is about the exception thrown while dynamically adding gridview. So the readers please keep in mind to add pageindexchanging event should be handled.
I hope this post helped you or given a nice thought. Thanks for reading.Please comment if you feel any issues on the same.


11 thoughts on “GridView Pagination issue when dynamically added….!

    Juthika said:
    March 30, 2010 at 4:47 am

    The GridView ‘gvDepartment’ fired event PageIndexChanging which wasn’t handled.

      abinjaik responded:
      March 30, 2010 at 8:05 am

      Hello Juthika,
      Thanks for comment. The PageIndexChanging event needs after pagination is done. This post is regarding the exception throwing when pagination is enabled for the mentioned scenario. Surely PageIndexChanging will be needed when the issue is fixed as per the mentioned solution.

    […] “Bei Ausnahmen anhalten” leider keine neuen Erkenntnisse gebracht hatten, bin ich durch diesen Artikel auf die Lösung […]

    sandy said:
    June 29, 2011 at 10:18 am

    who could have ever thought that this would solve it. many many thanks!!

    mellamokb said:
    December 8, 2011 at 11:59 pm

    Awesome, fixed it for me and saved a bunch of time – thanks!!

    Shilpa Salian said:
    October 16, 2012 at 10:54 am

    Thanks a ton. Saved a lot of time 🙂

    mahmoud farahat said:
    September 15, 2013 at 4:48 pm

    Thank You sir 🙂 , this was very helpful

    irfan said:
    October 22, 2013 at 10:53 am

    Thank you for the perfect solution

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s