Custom values for grid with automatic CRUD operations with two way data binding

12 posts, 0 answers
  1. Hendrik
    Hendrik avatar
    23 posts
    Member since:
    Jan 2008

    Posted 28 Sep 2010 Link to this post

    I have a grid bound to an EntityDataSource control. Both the EntityDataSource and the grid have automatic CRUD operations enabled. And using two way data binding everything works as expected. But there are a couple of database fields not on the form, who still need their values to be set.

    I thought something like this should do the trick:

    var item = e.Item as GridEditableItem;

    if
    (item == null)
        return;

    var values =
    new ListDictionary();
    item.ExtractValues(values);

    values.Add(
    "Field", "Value");
    e.Item.OwnerTableView.InsertItem(values);

    The above code is placed within the OnInsert- and OnUpdateCommand events of the grid. But it seems like the ListDictionary isn't updated with the onde containing the custom value. How can I achieve this? I can not imagine I'm the first one stumbling upon this, but I can't find anything on this subject on the forums.

    I hope someone can help me out...


    Hendrik
  2. Hendrik
    Hendrik avatar
    23 posts
    Member since:
    Jan 2008

    Posted 30 Sep 2010 Link to this post

    Waiting for a reply I started to search for a solution myself. It seems like I was looking in the wrong place to set default values of certain properties of the entity I was inserting. In stead of looking at the RadGrid and it's InsertCommand event, I had to look at the underlying datasource: the EntityDataSource control and it's OnInserting event. When the OnInserting event occurs you can cast the entity object passed on by the e.Entity argument to the appropriate object. After that It's very simple to set properties of the given object to the wished default values:

    var foo = e.Entity as Foo;
     
    if (foo == null)
        return;
     
    foo.Bar = "Any Value You Want";

    Another way to set values of properties is to use the InsertParameters collection of the EntityDataSource control. But I found it was impossible to set (or change) the default values of the parameters set at design time in code behind.. For some reason no changes to the parameter collection within the OnInserting event (in code behing) are passed through after the OnInserting event. So it's only possible to use the InsertParameters collection when you can set both the parameters themselves and the default values of these parameters at design time.

    Hope this helps for everyone else wanting to set default values of their business objects and still be able to use the code-less automatic CRUD operations of RadGrid.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Hendrik
    Hendrik avatar
    23 posts
    Member since:
    Jan 2008

    Posted 30 Sep 2010 Link to this post

    I'm still curious though about how to "inject" values of your own into the LisDictionary. Not all the fields on my edit form are two way data binding able. To give an example: my article entity has a collection property. The entities in this collection property are managed by two transferable RadListBox controls. So the TranswerTo control contains all the entities I would like to add to the article collection property (ListBoxItem.Value = entity.id). It seems impossible to make this work completely automated using the automatic CRUD operations. At some point I must iterate through the items and for each item add an entity object to the collection property of my article entity. Because the control itself is disposed at the the the OnInserting or OnUpdating event of the EntityDataSource control occurs, I can't fill the collection property at that point: there's no way to find out what items were in the RadListBox control.

    Of course it would be quite simple to achieve this by simply stopping to use automatic CRUD operations and handle the database operations myself within the appropriate commands of RadGrid. But the less code I have to write myself, the better :-)
  5. Iana Tsolova
    Admin
    Iana Tsolova avatar
    3388 posts

    Posted 01 Oct 2010 Link to this post

    Hello Hendrik,

    In order to achieve your goal, I suggest that you handle the OnInserting and OnUpdating server-side events and their set the values for the missing properies.

    Try it out and let me know how it goes.

    Greetings,
    Iana
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  6. Hendrik
    Hendrik avatar
    23 posts
    Member since:
    Jan 2008

    Posted 01 Oct 2010 Link to this post

    Like I said: at the point of the OnInserting or OnUpdating event the EditForm is not available anymore (as far as I know). So there is no way to check certain controls within the EditForm for the values the users has set.
  7. Iana Tsolova
    Admin
    Iana Tsolova avatar
    3388 posts

    Posted 04 Oct 2010 Link to this post

    Hello Hendrik,

    However on Inserting and Updating you should be able to see what values have the grid already passed ti the data source. Which indeed came from the controls in the edit form.

    Sincerely yours,
    Iana
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  8. Hendrik
    Hendrik avatar
    23 posts
    Member since:
    Jan 2008

    Posted 04 Oct 2010 Link to this post

    Controls not bound using the the "Bind" expression (two way data binding) will not pass their set values back to the data source, will they?
  9. matt
    matt avatar
    75 posts
    Member since:
    May 2008
  10. Iana Tsolova
    Admin
    Iana Tsolova avatar
    3388 posts

    Posted 05 Oct 2010 Link to this post

    Hello,

    Yes, you are right the only values from controls bound with the Bind() method will be automatically passed by the grid to the DataSource. So you can try extracting the desired values on UpdateCommand and save them into a public object so you can use them later in the OnUpdating event handler. Another option is to perform the update manually directly on UpdateCommand.

    All the best,
    Iana
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  11. Hendrik
    Hendrik avatar
    23 posts
    Member since:
    Jan 2008

    Posted 05 Oct 2010 Link to this post

    I think (or know) the only way to do this is to forget about the automatic CRUD operations for inserting and updating and handle these actions manually within the insert and update commands. Within these events I create the needed business object. Using the FindControl method I find the controls that don't support two-way data binding. And with the ExtractValues and UpdateValues I can still have the properties bound to controls who do support two-way data binding have set automatically.
  12. Mery
    Mery avatar
    1 posts
    Member since:
    Mar 2012

    Posted 19 Mar 2012 Link to this post

    how set the new value for a field in update command? i would set value not get. Thanks
  13. JD.
    JD. avatar
    37 posts
    Member since:
    Aug 2012

    Posted 06 Aug 2012 Link to this post

    Hendrik,

    Thanks for your post. My simiar issue of saving audit fields with values was achieved using EntityDataSource1_Inserting event.

    But even i am curious to know if i could have just passed these values in RadGrid's Insert Event and let the event do the rest of default steps. 

    Regards

Back to Top
UI for ASP.NET Ajax is Ready for VS 2017