Esc / Cancel and improper reset of data

2 posts, 0 answers
  1. Tyree
    Tyree avatar
    105 posts
    Member since:
    Dec 2006

    Posted 27 Mar 2012 Link to this post

    The default behavior of a column when Esc is pressed (when in edit mode) appears to set DataMemberBinding back to the same textual value it was prior to the edit. Though this is appropriate at times, it is more often innappropriate when working with entites with navigation properties and I would like to know how to change this behavior.

    Take the example of a collection of Customers, each with a navigation property of Contact. So the Customer has a property of ContactId which is a Contact object that has Name, Phone, etc. To show the Contact name in the grid one could do DataMemberBinding={Binding Contact.Name} (assuming the row context is the Customer). This looks fine, but of course if you try to edit this column you will be editing Contact.Name and not Customer.ContactId. So implement the CellEditingTemplate and make the edit template show whatever is appropriate to modify the Customer.ConactId which will in turn change Customer.Contact and reflect Customer.Contact.Name in column.

    The problem comes when you want to hit Esc after having edited that column. It will revert Customer.Contact.Name back to the textual value it was prior to the edit - which is now technically a different Contact. So now you have two Contacts with the same name. It should, instead, set Customer.ContactId back to its previous value but of course it knows nothing of what the appropriate action should be.

    If you define Contact.Name without a setter this causes the grid to not let you put the column in edit mode even though you have IsReadOnly=false and/or you have a CellEditTemplate defined. (this "feature" is rediculous).
    If you set the Mode=OneWay for the DataMemberBinding the grid does not respect this and edits the value as it desires.
    If you implement IEditableObject it still resets the value so I end up with the Contact.Name is changed, but the Customer resets properly. So every row that uses the changed Contact so the new Name.

    So, how do I define what value should be reset when Esc is pressed? I do not want to have to define CellTemplates on every single column and customize the filter on ever column just so I can set the DataMemberBinding to {Binding ContactId}. Surely there is a way to define the behavior.

    Assuming you want to show "Contact.Name", filter by "Contact.Name", but when Esc is pressed I want to reset "ContactId".

    In my case if when I pressed Esc it could trigger a command and pass in the "UniqueName" or the column then I could do the reset myself.

    I tried messing with a KeyboardCommandProvider but it does not truely expose all the commands its going to execute and therefore modifying them gives mixed results.
  2. Nedyalko Nikolov
    Nedyalko Nikolov avatar
    871 posts

    Posted 02 Apr 2012 Link to this post


    Sorry for the late answer.

    Generally when you need to edit such properties (that represent an object (not some primitive value)) no matter if this is a navigation property or not the recommended way to do this is via GridViewComboBoxColumn. However GridViewComboBoxColumn has some known issues when too many items are set as ComboBox.ItemsSource (since this will create as many comboboxes as many visible rows you have on the screen), so very simple workaround is to set CellTemplate to something lightweight visual object (such as TextBlock) and bind it to same value as GridViewComboBoxColumn.DisplayMemberPath. At the end we have a fast grid in view mode and the power of ComboBox editing in edit mode.

    You can change the editing value within RadGridView.CellEditEnded event. You can determine if this is a commit or cancel operation by checking GridViewCellEditEndedEventArgs.EditAction property.

    Let me know if there is something unclear or you need further assistance.

    Nedyalko Nikolov
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Back to Top