【问题标题】:Show the page with particular item in RadGrid在 RadGrid 中显示具有特定项目的页面
【发布时间】:2013-01-03 10:52:28
【问题描述】:

我有一个RadGrid,我使用DataSourceID 提供数据。 RadGrid 有分页,我想显示包含某些特定项目的页面。为此,我在数据中找到该项目的偏移量并设置页码:

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

我的问题是把这段代码放在哪里。在OnDataBound 中,我似乎无法访问数据源。如果我把它放在OnSelecting 中,则检索数据会产生设置页码的副作用。我应该扩展 GridTableView 来实现这个功能吗?我应该重写哪个方法?

【问题讨论】:

  • 我的方法似乎是错误的。为了让网格显示特定页面,它需要从数据源请求该页面。不可能让页码依赖于数据源,因为数据源已经依赖于页码了。

标签: c# asp.net data-binding radgrid


【解决方案1】:

我建议在OnSelecting 中计算index 值(这取决于数据),而页面索引可以在OnDataBoundPreRender 事件中设置。

【讨论】:

  • 这是有道理的。 OnSelecting 用于获取数据,index 值是数据的一部分。它没有解决我的问题,但让我意识到我的方法完全错误。
【解决方案2】:

我的用例是跳转到刚刚使用弹出编辑器插入的项目。这是我解决它的方法。我在标签中省略了不相关的属性。所有数据接线都由您决定,但这里是相关位。重要提示:使用 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;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-19
    • 2017-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-17
    相关资源
    最近更新 更多