C# 如何在gridview中转到特定记录
我在a.aspx中有多页记录的gridview。C# 如何在gridview中转到特定记录,c#,asp.net,gridview,C#,Asp.net,Gridview,我在a.aspx中有多页记录的gridview。 单击b.aspx中的按钮时,我需要转到gridview中包含该记录的特定页面。我假设您实际上想要转到特定记录。然后我将通过url参数(或会话)传递它的ID,然后使用该记录转到网格中的页面。这样更好,因为页面可能会更改(例如,使用排序) 假设您的网格列出了产品,并且您希望确保显示特定的产品,因为您刚刚在另一个页面上编辑了产品详细信息。还假设您的数据源是一个数据表(但这并不重要): // ///将产品绑定到GridView。 /// ///如有必要
单击b.aspx中的按钮时,我需要转到gridview中包含该记录的特定页面。我假设您实际上想要转到特定记录。然后我将通过url参数(或会话)传递它的ID,然后使用该记录转到网格中的页面。这样更好,因为页面可能会更改(例如,使用排序) 假设您的网格列出了产品,并且您希望确保显示特定的产品,因为您刚刚在另一个页面上编辑了产品详细信息。还假设您的
数据源
是一个数据表
(但这并不重要):
//
///将产品绑定到GridView。
///
///如有必要,要显示的ProductID也会更改页面索引
私有void BindProductGrid(int-ProductID=-1)
{
DataTable TBLPProducts=getAllProducts();
GridProducts.DataSource=tblProducts;
bool needsPaging=(tblProducts.Rows.Count/GridProducts.PageSize)>1;
if(ProductID==-1)
{
this.GridProducts.PageIndex=0;
this.GridProducts.SelectedIndex=-1;
}
其他的
{
int selectedIndex=tblProducts.AsEnumerable()
.Select((行,索引)=>new{Row,Index})
.Single(x=>x.Row.Field(“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();
}
我假设您实际上想要转到特定的记录。然后我将通过url参数(或会话)传递它的ID,然后使用该记录转到网格中的页面。这样更好,因为页面可能会更改(例如,使用排序)
假设您的网格列出了产品,并且您希望确保显示特定的产品,因为您刚刚在另一个页面上编辑了产品详细信息。还假设您的数据源
是一个数据表
(但这并不重要):
//
///将产品绑定到GridView。
///
///如有必要,要显示的ProductID也会更改页面索引
私有void BindProductGrid(int-ProductID=-1)
{
DataTable TBLPProducts=getAllProducts();
GridProducts.DataSource=tblProducts;
bool needsPaging=(tblProducts.Rows.Count/GridProducts.PageSize)>1;
if(ProductID==-1)
{
this.GridProducts.PageIndex=0;
this.GridProducts.SelectedIndex=-1;
}
其他的
{
int selectedIndex=tblProducts.AsEnumerable()
.Select((行,索引)=>new{Row,Index})
.Single(x=>x.Row.Field(“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();
}
为什么需要转到该页面?我假设您实际上想要转到特定的记录。然后我将通过url参数(或会话)传递它的ID,然后使用该记录转到网格中的页面。这是更好的,因为页面可能会改变(例如排序)。我在gridview中有1000页的海量数据。我需要在另一个页面中编辑网格视图记录。当我按下“后退”按钮时,这是开始页,我发现很难返回到特定的记录。所以实际上,正如我所想的那样。您使用的是VB.NET还是C#?为什么需要转到该页面?我假设您实际上想要转到特定的记录。然后我将通过url参数(或会话)传递它的ID,然后使用该记录转到网格中的页面。这是更好的,因为页面可能会改变(例如排序)。我在gridview中有1000页的海量数据。我需要在另一个页面中编辑网格视图记录。当我按下“后退”按钮时,这是开始页,我发现很难返回到特定的记录。所以实际上,正如我所想的那样。你是在用VB.NET还是C?是的,你的答案对我很有帮助谢谢这就是我一直在寻找的答案是的,你的答案对我很有帮助谢谢这就是我一直在寻找的答案
/// <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();
}