JG Vimalan's Blog

It is all about coding!

SPGridView Paging

The following code will give you an idea of using SPGridView and performe paging in it.

 using System;

using System.Collections.Generic;

using System.Text;

using System.Data;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using Microsoft.SharePoint;

using Microsoft.SharePoint.WebControls;

using Microsoft.SharePoint.Utilities;

namespace MySPGridViewPagingSample {

public class MySPGridView : WebPart {

 SPGridView _MyDetails= null;

protected override void CreateChildControls() {

 SPSite site = new SPSite(“http://wss-MySite/”); //Open a site

 SPWeb web = site.OpenWeb();

SPList list = web.Lists[“MyList”]; //Get the items from a list.

 _MyDetails = new SPGridView();

MyDetails.AutoGenerateColumns = false;

_MyDetails.AllowPaging = true;

_MyDetails.PageSize = 5;

_MyDetails.PageIndexChanging += new GridViewPageEventHandler(_MyDetails_PageIndexChanging);

BoundField field = new BoundField();

field.HeaderText = “Title”;

field.DataField = “Title”;

field.ShowHeader = true;


BoundField field2 = new BoundField();

field2.DataField = “Status”;

field2.HeaderText = “Status”;


_MyDetails.DataSource = list.Items.GetDataTable(); this.Controls.Add(_MyDetails);

_MyDetails.PagerTemplate = null;



void _MyDetails_PageIndexChanging(object sender, GridViewPageEventArgs e)


_MyDetails.PageIndex = e.NewPageIndex;



public override void RenderControl(HtmlTextWriter writer)



_MyDetails.RenderControl(writer); } } }

The output will be,



September 27, 2009 - Posted by | SharePoint


  1. Hi, at the moment I am browsing with my iphone four new style tips and so I’ve identified your web page. Your style is actually straightforward but also compare it in your posts. I imagine I would use it for my primary draft. Thank you for this concept!

    Comment by Horacio Lenharr | November 8, 2010 | Reply

  2. I couldnt find this site and luckily I found it again. right now am at my library I saved this so that I will look at it later thank you

    Comment by bklyn escort | November 12, 2010 | Reply

  3. Valuable information and excellent design you got here! I would like to with thanks for sharing your thoughts and time into the stuff you blog post!! Thumbs up! I just hope to have understood this the way it was meant. Respectfully, Edith.

    Comment by Edith | November 12, 2010 | Reply

  4. Of course, what a great site and informative posts, I will add backlink – bookmark this site? Regards, Reader.

    Comment by Trudie Prevet | November 13, 2010 | Reply

  5. cool stuff. thanks for sharing!

    Comment by Tim | November 16, 2010 | Reply

  6. excellent! u saved my day!

    Comment by Sharice Wadick | November 17, 2010 | Reply

  7. thanks

    Comment by Carlton Carrere | November 18, 2010 | Reply

  8. neatly explained. can you post an example for sorting in spgridview? can we use ajax/jquery for better results?

    Comment by Sammy Mcgovern | November 19, 2010 | Reply

  9. how can i add controls like textbox, dropdown in the spgridview?

    Comment by Paula | November 23, 2010 | Reply

  10. Interesting. Perhaps you could share more with us?

    Comment by pat | November 27, 2010 | Reply

  11. can you show an example of sorting?

    Comment by Murali Kannan | November 28, 2010 | Reply

  12. Hats off to you, gifted writer. You are fantastic.

    Comment by Debora | December 23, 2010 | Reply

  13. thnks

    Comment by Jersey | February 16, 2011 | Reply

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

%d bloggers like this: