Skip to content

Instantly share code, notes, and snippets.

@ericjuden
Last active December 11, 2015 20:29
Show Gist options
  • Save ericjuden/4655906 to your computer and use it in GitHub Desktop.
Save ericjuden/4655906 to your computer and use it in GitHub Desktop.
ASP.NET/C# GridView Rank column
public partial class Leaders : System.Web.UI.Page {
private int rank = 0; // Rank of the current record in GridView
private int currentPage = 0; // tracks current page in GridView
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
// Find label control to update
Label lblRank = (Label)e.Row.FindControl("lblRank");
// On page reload, rank is reset to 0
if (rank == 0) {
// Only run this on subsequent pages
if($currentPage > 0){
// Set rank to current index of page * the number of records to display on GridView page
rank = currentPage * GridView1.PageSize;
}
}
// Make sure we actually found our label
if (lblRank != null) {
// Increment rank by 1
rank += 1;
// Set rank label to our new rank value
lblRank.Text = rank.ToString();
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) {
// Set current page = to index of new GridView page
currentPage = e.NewPageIndex;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment