我有一个带有 ASP.net GridView 的网页(带分页)。 GridView 有超过 10,000 行。 每行都有一个“ID”字段。
这是过于简单化了,但假设我希望用户能够在文本字段中键入任意行 ID,并转到 GridView,页面索引自动设置为包含指定行的页面索引身份证。
我已尝试以下方法,但不起作用:
DataSet ds = ReportManager.GetDevices(AdHocQuery);
lblRecordCount.Text = String.Format("{0:#,0}", ds.Tables[0].Rows.Count);
string sortDirection = AdHocQuery.SortDirection == "Descending" ? " DESC" : " ASC";
DataView dv = new DataView(ds.Tables[0], string.Empty, AdHocQuery.SortColumnName + " " + sortDirection, DataViewRowState.CurrentRows);
gvAsset.DataSource = dv;
gvAsset.DataBind();
// If there was a device ID passed in the Query String, then change the page to the page that contains the given ID.
if (!string.IsNullOrEmpty(DeviceId))
{
// First, find the row number of the given device Id
int rowNumber = 0;
int i;
for (i = 0; i < dv.Table.Rows.Count; i++)
{
DataRow row = dv.Table.Rows[i];
if (row.Field<Guid>("deviceId").ToString().ToUpper() == DeviceId.ToUpper())
{
rowNumber = i;
break;
}
}
// The page index is going to be the row Number divided by the Page size
// For example, if the page size is 10...
// row 5 is on page 0,
// row 15 is on page 1,
// row 28 is on page 2, and so on
int pageIndex = rowNumber / gvAsset.PageSize;
gvAsset.PageIndex = pageIndex;
此代码可以编译,但它始终显示 GridView 的错误页面。
我该如何编码才能显示正确的页面?
我相信您需要调用 DataBind() 才能对 PageIndex 属性进行更改。
绑定网格之前调用
gvAsset.PageIndex = pageIndex
你可以尝试这样的事情。它在我的情况下有效。
//assuming there are SomeIDs from 1 to 100 in the database
//assuming pagesize of gridview is 10
DataTable table = GetSomeDataFunction(); //your data retrieval method
for (int i = 0; i < table.Rows.Count; i++)
{
if (table.Rows[i].Field<int>("SomeID") == 11) //11 = ID you're looking for
{
GridView1.PageIndex = (i / GridView1.PageSize);
break;
}
}
GridView1.DataSource = table;
GridView1.DataBind();
我已经找到了我自己问题的答案。 在四处寻找答案后,我发现如果我想枚举代码隐藏中的行列表,我需要调用
DataView.GetEnumerator()
函数。 代码如下:
DataSet ds = ReportManager.GetDevices(AdHocQuery);
lblRecordCount.Text = String.Format("{0:#,0}", ds.Tables[0].Rows.Count);
string sortDirection = AdHocQuery.SortDirection == "Descending" ? " DESC" : " ASC";
DataView dv = new DataView(ds.Tables[0], string.Empty, AdHocQuery.SortColumnName + " " + sortDirection, DataViewRowState.CurrentRows);
gvAsset.DataSource = dv;
gvAsset.DataBind();
// If there was a device ID passed in the Query String, then change the page to the page that contains the given ID.
if (!string.IsNullOrEmpty(DeviceId))
{
// First, find the row number of the given device Id
int rowNumber = 0;
int i = 0;
System.Collections.IEnumerator iterator = dv.GetEnumerator();
while (iterator.MoveNext())
{
DataRowView drv = (DataRowView)iterator.Current;
DataRow row = drv.Row;
if (row.Field<Guid>("deviceid").ToString().ToUpper() == DeviceId.ToUpper())
{
rowNumber = i;
break;
}
i++;
}
// The page index is going to be the row Number divided by the Page size
// For example, if the page size is 10...
// row 5 is on page 0,
// row 15 is on page 1,
// row 28 is on page 2, and so on
int pageIndex = rowNumber / gvAsset.PageSize;
gvAsset.PageIndex = pageIndex;
}