Saturday, 1 October 2011

How can you enable automatic paging in DataGrid?


In order to enable paging in Datagrid you need to (very short version):
  Set the "AllowPaging" property to true.
  In PageIndexChanged event handler set the current PageIndex clicked.
Below is an example in C#:
//Declaring DataGrid
protected System.Web.UI.WebControls.DataGrid DataGrid1;
//Binding DataGrid with a Data Source (DataSet in our Case)
DataGrid1.DataSource = dataSet1;
DataGrid1.DataBind();
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.GridPageIndexChanged);
//Implement the EventHandler
private void GridPageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
//Bind the DataGrid again with the Data Source
DataGrid1.DataSource = dataSet1;
DataGrid1.DataBind();
}


No comments:

Post a Comment