Programming Journal C#, Java, SQL and to a lesser extent HTML, CSS, XML, and regex. I made this so other programmers could benefit from my experience.

Sunday, November 25, 2007

How to implement Select on a GridView1 with Paging enabled.

How to implement Select on a GridView1 with Paging enabled based on a DataTable.

protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
// e.NewSelectedIndex -> 0 to n
// GridView1.PageCount -> (n-1) always
// GridView1.PageIndex -> 0 to Page#
int rowN = 1 + ((GridView1.PageCount-1) * GridView1.PageIndex + e.NewSelectedIndex);
DetailsView1.HeaderText = "Details View of Row "+ rowN.ToString();
DetailsView1.PageIndex = rowN - 1;
GridView1.SelectedIndex = rowN - 1;
DetailsView1.DataSource = Cache["dt"];
DetailsView1.DataBind();
}

No comments: