DataGrid Pagination in Webparts

Step 1: Set up your CreateChildControls

protected override void CreateChildControls()
{
dGrid = new DataGrid();
// write code to set up the data grid
// with your columns etc.

dGrid .PageIndexChanged +=new DataGridPageChangedEventHandler(dGrid _PageIndexChanged);

base.Controls.Add(dGrid);
base.CreateChildControls();


}

Step 2: Create the PageIndexChanged Event handler
private void dGrid_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e )
{
dGrid .CurrentPageIndex = e.NewPageIndex;
dGrid.DataSource = dtSource;
dGrid.DataBind();
}

Step 3: Render the DataGrid

protected override void RenderWebPart(HtmlTextWriter output)
{
output.Write(SPEncode.HtmlEncode(Text));
dGrid.RenderControl(output);
}