2011-09-02 28 views
1

我有一個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的錯誤頁面。

我該如何編碼,以便顯示正確的頁面?

回答

0

我已經找到了自己的問題的答案。在搜索答案後,我發現如果我想枚舉代碼隱藏中的行列表,我需要調用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; 
     } 
0

我相信你需要調用DataBind()以便你改變PageIndex屬性到位。

+0

在我發佈的代碼片段中,'PageIndex'屬性被設置,但它被設置爲錯誤的值。我認爲這與分揀有關。 –

0

呼叫gvAsset.PageIndex = pageIndex結合電網

0

之前,您可以嘗試這樣的事情。它符合我的情況。

//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(); 
+0

你在哪裏聲明和初始化'table'?我發現表中的條目與我在頁面實際呈現時看到的順序不同。 –

+0

並不是一個完整的例子。我將修改以包含DataTable初始化。 –

+0

@米粉餅乾:解答已更新。 –

相關問題