我有一個ASP.net GridView(帶分頁)的網頁。 GridView擁有超過10,000行。每行都有一個「ID」字段。將ASP.net GridView的頁面設置爲包含給定行的頁面的有效方法是什麼?
這是一個過分簡單化,但假設我希望用戶能夠在文本字段中輸入一個仲裁行ID,並將其帶到GridView,頁面索引自動設置爲包含該行的頁面的索引與指定的ID。
我曾嘗試以下,但它不工作:
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的錯誤頁面。
我該如何編碼,以便顯示正確的頁面?
在我發佈的代碼片段中,'PageIndex'屬性被設置,但它被設置爲錯誤的值。我認爲這與分揀有關。 –