This is a migrated thread and some comments may be shown as answers.

Telerik RadGrid - Filtering while Editing does not remember correct row

3 Answers 75 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Joey
Top achievements
Rank 1
Joey asked on 07 Jun 2013, 02:41 AM
Hi guys. 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.

(this is a repost of http://www.telerik.com/community/forums/aspnet-ajax/grid/how-to-cancel-edit-mode-when-filtering.aspx, i couldn't figure out how to change the topic title or delete that post, sorry)

3 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 07 Jun 2013, 12:42 PM
Hello,

we are not able change the title or Delete this post.

Telerik Admin delete your post letter.

Thanks,
Jayesh Goyani
0
Jayesh Goyani
Top achievements
Rank 2
answered on 07 Jun 2013, 01:04 PM
Hello,

Please try with below code snippet.

<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>
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.

Thanks.
Jayesh Goyani
0
Joey
Top achievements
Rank 1
answered on 10 Jun 2013, 01:30 AM
Thanks Jayesh, it works great. I used a combintation of your suggestion with Princy's one (http://www.telerik.com/community/forums/aspnet-ajax/grid/how-to-cancel-edit-mode-when-filtering.aspx). Anyway, I'm curious why this feature (the ability to remember edit row on filtering) isn't automatically added into the control? Will it be added in the future?
Tags
Grid
Asked by
Joey
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Joey
Top achievements
Rank 1
Share this question
or