Search This Blog

Friday, December 02, 2005

DataGrid Pagination in Webparts

Today I had an issue with a data grid on a web part not paginating. After a bit of merry-go-around the web I could figure out how to do it.

The following is the sequence of steps.

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);
}



You should be in business. Happy coding guys.

No comments: