如何去在gridview的特定记录 [英] How to go to particular record in gridview
问题描述
我有在a.aspx记录多个页面的GridView。
当我b.aspx点击一个按钮,我需要去包含在GridView的记录某个特定页面。
I have gridview with multiple pages of records in a.aspx. When I click a button in b.aspx I need to go to a specific page that contains the record in the gridview.
推荐答案
我假设你真正想要去一个特定的记录来代替。然后我会通过它通过URL参数(或会话)的ID,然后进入该页面与该记录的网格。这是更好,因为页面可能会发生变化(f.e与排序)。
I assume that you actually want to go to a specific record instead. Then i would pass it's ID via url-parameter (or session) and then go to the page in the grid with this record. That's is better because the page might change(f.e. with sorting).
假设你的网格列出的产品,并要确保perticular产品是因为您刚才编辑的另一页上的产品的详细信息。同时假设你的数据源
是数据表
(但它其实并不重要):
Assuming that your grid lists products and you want to ensure that a perticular product is shown because you've just edited the product-details on another page. Also assuming that your DataSource
is a DataTable
(but it doesn't really matter):
/// <summary>
/// Binds the products-GridView.
/// </summary>
/// <param name="ProductID">the ProductID to be displayed, changes also the PageIndex if necessary</param>
private void BindProductGrid(int ProductID = -1)
{
DataTable tblProducts = getAllProducts();
GridProducts.DataSource = tblProducts;
bool needsPaging = (tblProducts.Rows.Count / GridProducts.PageSize) > 1;
if (ProductID == -1)
{
this.GridProducts.PageIndex = 0;
this.GridProducts.SelectedIndex = -1;
}
else
{
int selectedIndex = tblProducts.AsEnumerable()
.Select((Row, Index) => new { Row, Index })
.Single(x => x.Row.Field<int>("ProductID") == ProductID).Index;
int pageIndexofSelectedRow = (int)(Math.Floor(1.0 * selectedIndex / GridProducts.PageSize));
GridProducts.PageIndex = pageIndexofSelectedRow;
GridProducts.SelectedIndex = (int)(GridProducts.PageIndex == pageIndexofSelectedRow ? selectedIndex % GridProducts.PageSize : -1);
}
GridProducts.DataBind();
}
这篇关于如何去在gridview的特定记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!