Question

I have implemented a RadGrid with Edit and Insert Mode and filter capabilities.

When filtering the RadGrid while in edit mode, the edited row seems to be based on the row number, eg, when I'm editing row number 3, and while editing, the grid is filtered, the edited row remains at row number 3 even though the row of the record I'm currently editing may have changed.

For Example, if I'm doing auto CRUD on this table with in place editing

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

and the row being edited is the 2nd one ((02) codeY)

if a filter (using the RadGrid default filter) is done on Code "EqualTo" 'codeY' such that the result becomes

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

the edited row is still the 2nd one ((03) codeY) even though the row originally being edited is ((02) codeY)

Is this the expected behaviour, or is there are way to instruct the RadGrid to look for the record to set the edit mode on that particular record again? If not, is there a way to automatically cancel edit mode/insert mode just before filtering? Or to disable all filtering controls while user is in edit/insert mode? Thanks for reading.

Was it helpful?

Solution

Please try with the below code snippet.

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();
    }
}

Let me know if any concern.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top