Telerik RadGrid - 编辑时过滤不记得正确的行

时间:2013-06-07 02:27:54

标签: telerik radgrid

我已经实现了一个带有编辑和插入模式以及过滤功能的RadGrid。

在编辑模式下过滤RadGrid时,编辑的行似乎基于行号,例如,当我编辑第3行时,编辑时,网格被过滤,编辑的行保留在行即使我正在编辑的记录行可能已更改,但是数字3。

例如,如果我在此表上使用就地编辑进行自动CRUD

(id) (code)
-----------------------
(01) codeX
(02) codeY
(03) codeY

,正在编辑的行是第二行((02)codeY)

如果在代码“EqualTo”'codeY'上完成了一个过滤器(使用RadGrid默认过滤器),结果变为

(id) (code)
-----------------------
(02) codeY
(03) codeY

编辑的行仍然是第二个((03)codeY),即使最初编辑的行是((02)codeY)

这是预期的行为,还是有办法指示RadGrid查找记录以再次在该特定记录上设置编辑模式?如果没有,有没有办法在过滤之前自动取消编辑模式/插入模式?或者在用户处于编辑/插入模式时禁用所有过滤控件?谢谢阅读。

1 个答案:

答案 0 :(得分:1)

请尝试使用以下代码段。

ASPX

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" OnNeedDataSource="RadGrid1_NeedDataSource"
    AllowFilteringByColumn="true" OnItemCommand="RadGrid1_ItemCommand" OnPreRender="RadGrid1_PreRender">
    <MasterTableView EditMode="InPlace" DataKeyNames="ID" CommandItemDisplay="Top">
        <Columns>
            <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
            </telerik:GridBoundColumn>
            <telerik:GridEditCommandColumn>
            </telerik:GridEditCommandColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

ASPX.CS

public List<int> EditIDs
{
    get
    {
        if (ViewState["EditID"] != null)
        {
            return (List<int>)ViewState["EditID"];
        }
        else
        {
            return new List<int>();
        }
    }
    set { ViewState["EditID"] = value; }
}

public bool IsFilterCommandFire { get; set; }

protected void Page_Init(object sender, EventArgs e)
{

}
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        EditIDs = new List<int>();
    }
}
protected void Page_PreRender(object sender, EventArgs e)
{

}
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    dynamic data = new[] {
        new { ID = 1, Name ="Name1"},
        new { ID = 2, Name = "Name2"},
        new { ID = 3, Name = "Name3"},
         new { ID = 4, Name = "Name4"},
        new { ID = 5, Name = "Name5"},
        new { ID = 26, Name = "Name26"}
    };

    RadGrid1.DataSource = data;

}
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == RadGrid.FilterCommandName)
    {
        IsFilterCommandFire = true;
    }
    else if (e.CommandName == RadGrid.EditCommandName)
    {
        int ID = Convert.ToInt32((e.Item as GridEditableItem).GetDataKeyValue("ID"));
        EditIDs.Add(ID);
    }
    else if (e.CommandName == RadGrid.CancelCommandName)
    {
        int ID = Convert.ToInt32((e.Item as GridEditableItem).GetDataKeyValue("ID"));
        EditIDs.Remove(ID);
    }
}
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    if (IsFilterCommandFire)
    {
        foreach (GridDataItem item in RadGrid1.Items)
        {
            if (EditIDs.Contains(Convert.ToInt32(item.GetDataKeyValue("ID"))))
            {
                item.Edit = true;
            }
            else
            {
                item.Edit = false;
            }
        }
        RadGrid1.Rebind();
    }
}

如果有任何疑虑,请告诉我。