Sunday, September 21, 2008

DataGrid Paging in a Web Part

So this is a simple .NET one, but I ran in to this the other day working on a custom Web Part.

The issue I ran into was doing paging on a DataGrid inside a simple custom Web Part. All of the Web Part's rendering is happening inside of CreateChildControls so I have no aspx/ascx file to set the properties of my DataGrid (which is being created in code) but this is what much of the documentation out there says to do. This is how you do it entirely in code:

For paging to work (besides having the AllowPaging property set) you must specify the method to get called when the paging button is clicked:


myDataGrid.PageIndexChanged += new DataGridPageChangedEventHandler(this.GridChangeMethod);


You can also specify any of the other properties that you would normally do in the aspx file:

myDataGrid.PagerStyle.Mode = PagerMode.NextPrev;
myDataGrid.PagerStyle.NextPageText = "Next >";
myDataGrid.PagerStyle.PrevPageText = "< Prev";

Ref: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datagrid.onpageindexchanged.aspx

No comments: