2011-09-02 29 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

@米粉饼干:解答已更新。 –

相关问题