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

Change value before update with Linq and Automatic Updates

4 Answers 210 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 08 Jul 2008, 09:36 PM
I have a RadGrid bound to a LinqDataSource for Products.  I want to set 1 of the product values manually (in code behind) while the user can set the rest using bound controls.  THe problem is I can not figure out how to manually set the value.

I tried to create a hidden bound column (Display=false) for the field, and then in the UpdateCommand event of the RadGrid, I did the following:

        protected void rgProducts_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)  
        {  
            // re show the edit column  
            rgProducts.Columns[0].Visible = true;   
 
            // set some values manually  
            GridEditFormItem item = e.Item as GridEditFormItem;  
            item.ParentItem["UpdatedBy"].Text = UserId.ToString();  
        } 

I stepped through the code and the value is set properly, but it never gets updated in the database.  I can only assume my approach is not correct and further searches through the forums has yielded no results either.

How can I get access to the current linq object being updated and set this one member in the codebehind when the user updates so that my value is saved along with their values?

Thanks so much!

Michael

4 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 09 Jul 2008, 07:54 AM
Hello Michael,

This line:

item.ParentItem["UpdatedBy"].Text

will change the text of the TableCell - you need to change the value of desired column editor.

Here is an example for TextBox:

if(e.Item is GridEditFormItem && e.Item.IsInEditMode)
{
    ((e.Item as GridEditFormItem)["UpdatedBy"].Controls[0] as TextBox).Text = UserId.ToString();
}

Best wishes,
Vlad
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Michael
Top achievements
Rank 1
answered on 09 Jul 2008, 11:59 AM
Vlad,

This didn't work.  I get an exception:

"Specified argument was out of the range of valid values. Parameter name: index"

This exception refers to the Controls[0].  I probably should have put my column definition so you could see that as well:

<telerik:GridBoundColumn DataField="UpdatedBy" HeaderText="UpdatedBy"

UniqueName="UpdatedBy" Display="false" ReadOnly="true">

</telerik:GridBoundColumn>

Any other options?

Also, since I am using LINQ, I assume that the RadGrid with AutomaticUpdates creates the Linq entity, updates its values from the grid and then calls SubmitChanges()... is there any way to get direct access to the Linq entity?  In this case it is an entity based off of a tables called "Products" .  It would seem to be most effecient and flexible to be able to simply acquire the instance and manually do "Products.UpdatedBy = UserId.ToString();".  I suppose that type of programmatic access would be a feature request...

Any other way to update this columns value before update?

Thanks for all your help!

Michael
0
Michael
Top achievements
Rank 1
answered on 09 Jul 2008, 01:23 PM
This is what I would love to be able to do in the UpdateCommand event:

            // set UpdatedBy manually  
            if (e.Item is GridEditFormItem && e.Item.IsInEditMode)  
            {  
                (e.Item.DataItem as Product).UpdatedBy = UserId;  
            } 

To have that kind of direct access would be hugely efficient.

Until such time that is possible, please advise on a solution to solve this need.

Thanks!

Michael

0
Michael
Top achievements
Rank 1
answered on 10 Jul 2008, 03:37 AM
Well, I ended up just discarding going with Automatic updates.  I am now handling the update all manually with LINQ in the the UpdateCommand event.  It wasn't much code and gave me all the flexibility I needed.  As always, thanks for your assistance!

If you have thoughts on providing programmatic access to the Linq entity being updated in the UpdateCommand during Automatic updates, let me know!  I still think that would be cool!!

Love your products!

Thanks!

Mike
Tags
Grid
Asked by
Michael
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Michael
Top achievements
Rank 1
Share this question
or