ASP.NET GridView Paging and Sorting

Overview

GridView renders tabular data with built-in paging and sorting when bound to SqlDataSource or ObjectDataSource. Users click column headers to sort and footer links to change pages.

For large datasets, server-side paging is mandatory.

Implementation

Set AllowPaging="true" PageSize="20" and handle PageIndexChanging to cancel default, set PageIndex, and rebind. Sorting: AllowSorting="true", handle Sorting, adjust sort expression on the data source.

Custom paging passes startRowIndex and maximumRows from ObjectDataSource.

When implementing guidance from ASP.NET GridView Paging and Sorting, start in a controlled environment that mirrors production versions of operating systems, runtimes, and network policies. Capture a baseline before changes: export configs, snapshot VMs, or tag releases in source control so rollback stays straightforward if behavior regresses.

Document prerequisites, expected outcomes, and verification steps in a short runbook. Automated checks—smoke tests, health endpoints, or query validations—catch regressions early when platforms receive patches. Security belongs in every workflow: apply least privilege, rotate secrets, and review audit logs after deployment.

If results differ across machines, compare environment variables, permission models, time zones, and regional settings. Intermittent issues often trace to caching layers, stale DNS, or duplicated services bound to the same port.

Example

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) {
  GridView1.PageIndex = e.NewPageIndex;
  BindGrid();
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) {
  SortExpression = e.SortExpression;
  SortDirection = SortDirection == "ASC" ? "DESC" : "ASC";
  BindGrid();
}

Tips

  • Virtual scrolling needs third-party grids.
  • ViewState grows with wide grids—disable where possible.
  • Use PagedCollection for APIs in Core.
  • Sanitize sort columns against SQL injection.
  • Re-verify after reboots, certificate renewals, or failover exercises.
  • Align monitoring and alerts with the failure modes described in this guide.
  • Keep vendor documentation links handy for breaking changes between versions.
  • Pair automation with a manual spot check during initial production rollout.