2013-01-03 30 views
0

我有一個RadGrid,我使用DataSourceID提供數據。 RadGrid有分頁功能,我想顯示包含某些特定項目的頁面。要做到這一點,我覺得在數據項目的偏移量,並設置頁碼:在RadGrid中顯示特定項目的頁面

var index = dataSource.Count(t => t.Id > _selectedTickId); 
var page = index/rgTicks.PageSize; 
rgTicks.CurrentPageIndex = page; 

我的問題是在哪裏把這個代碼。在OnDataBound我似乎沒有訪問數據源。如果我把它放在OnSelecting中,檢索數據有一個設置頁碼的副作用。我應該擴展GridTableView來實現這個功能嗎?我應該重寫哪種方法?

+0

我的做法似有不妥。爲了網格顯示特定頁面,它需要從數據源請求該頁面。不可能讓頁碼取決於數據源,因爲數據源已取決於頁碼。 – Sjoerd

回答

1

我會建議來計算OnSelectingindex值(這是數據依賴的),而頁面索引可以在OnDataBoundPreRender事件來設置。

+0

這是有道理的。 OnSelecting用於獲取數據,「索引」值是數據的一部分。這並沒有解決我的問題,但讓我意識到我完全錯誤的做法。 – Sjoerd

0

我的用例是跳轉到剛剛使用彈出編輯器插入的項目。這是我解決它的方法。我在標籤中省略了不相關的屬性。所有的數據連線取決於你,但這裏是相關的位。重要提示:使用DataKeyNames可以避免在GridDataItem中挖掘一個值。

在頁面我有:

<telerik:RadGrid ID="rgItems" runat="server" AllowPaging="true" 
     OnNeedDataSource="rgItems_NeedDataSource" 
     OnPreRender="rgItems_PreRender" 
     OnInsertCommand="rgItems_InsertCommand"> 
     <MasterTableView 
      CommandItemDisplay="Top" 
      CommandItemSettings-AddNewRecordText="Add New Item" 
      CommandItemSettings-ShowAddNewRecordButton="True" 
      DataKeyNames="IntItemId" 
      EditMode="popup" 
      EditFormSettings-PopUpSettings-Modal="true">        

而且在後面的代碼:

private bool itemInserted = false; 

protected void rgItems_InsertCommand(object sender, GridCommandEventArgs e) 
{ 
    itemInserted = true; 
} 

protected void rgItems_PreRender(object sender, EventArgs e) 
{ 
    if (itemInserted) 
    { 
     // Select the record and set the page 
     int LastItem = 0; // Put code to get last inserted item here 
     int Pagecount = rgItems.MasterTableView.PageCount; 
     int i = 0; 
     GridDataItem GDI = null; 
     while (i < Pagecount) 
     { 
      rgItems.CurrentPageIndex = i; 
      rgItems.Rebind(); 
      GDI = rgItems.MasterTableView.FindItemByKeyValue("IntItemId", LastItem); 
      if (GDI != null) break; // IMPORTANT: Breaking here if the item is found stops you on the page the item is on 
      i++; 
     } 
     if (GDI != null) GDI.Selected = true; // Optional: Select the item 
     itemInserted = false; 
    } 
} 
相關問題