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

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

11 Answers 174 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Hendrik
Top achievements
Rank 2
Hendrik asked on 28 Sep 2010, 08:39 AM
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

11 Answers, 1 is accepted

Sort by
0
Hendrik
Top achievements
Rank 2
answered on 30 Sep 2010, 11:57 AM
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.
0
Hendrik
Top achievements
Rank 2
answered on 30 Sep 2010, 03:36 PM
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 :-)
0
Iana Tsolova
Telerik team
answered on 01 Oct 2010, 03:03 PM
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
0
Hendrik
Top achievements
Rank 2
answered on 01 Oct 2010, 09:02 PM
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.
0
Iana Tsolova
Telerik team
answered on 04 Oct 2010, 10:39 AM
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
0
Hendrik
Top achievements
Rank 2
answered on 04 Oct 2010, 10:49 AM
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?
0
matt
Top achievements
Rank 1
answered on 04 Oct 2010, 08:28 PM
0
Iana Tsolova
Telerik team
answered on 05 Oct 2010, 11:03 AM
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
0
Hendrik
Top achievements
Rank 2
answered on 05 Oct 2010, 11:43 AM
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.
0
Mery
Top achievements
Rank 1
answered on 19 Mar 2012, 10:52 AM
how set the new value for a field in update command? i would set value not get. Thanks
0
JD.
Top achievements
Rank 1
answered on 06 Aug 2012, 03:08 PM
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

Tags
Grid
Asked by
Hendrik
Top achievements
Rank 2
Answers by
Hendrik
Top achievements
Rank 2
Iana Tsolova
Telerik team
matt
Top achievements
Rank 1
Mery
Top achievements
Rank 1
JD.
Top achievements
Rank 1
Share this question
or