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

RADGRID Inserting logic for EntityDataSource

5 Answers 175 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dan
Top achievements
Rank 1
Dan asked on 28 Feb 2011, 06:37 PM
Hi,

I need to control field values before a record is inserted or updated from a Radgrid using an EntityDataSource.

I want to do something as simple as setting the value of one field based on the value of another, and expected to see Radgrid events for OnInserting and OnUpdating (where logic could be used before the transaction is committed). With the traditional ASP GridView, this could be done on the OnUpdating event.

Thanks,
Dan

5 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 01 Mar 2011, 05:37 AM
Hello Dan,

You can either use InsertCommand/UpdateCommand or ItemCommand event with appropriate CommandName . For more information on this please refer the following documentations.
http://www.telerik.com/help/aspnet-ajax/grid-command-reference.html
http://www.telerik.com/help/aspnet-ajax/grid-insert-update-delete-at-database-level.html

Thanks,
Princy.
0
Dan
Top achievements
Rank 1
answered on 01 Mar 2011, 02:42 PM
Thanks,

I've played around with this and can't get it to work.

I've tried things such as:

        protected void rgEmployees_InsertCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item;
            string resignationDate = (insertedItem["ResignationDate"].Controls[0] as TextBox).Text;
            if (resignationDate == "") (insertedItem["Outprocessed"].Controls[0] as TextBox).Text = "false";
            else (insertedItem["Outprocessed"].Controls[0] as TextBox).Text = "true";
        }

Is this on the right track? Or are you saying that I can't perform logic to directly modify the values used in an insert/update, but I have to recreate the entire sql insert/update statement as in the example?

I'm guessing there's an easier way, since even the ASP gridview allows you to insert logic before and after inserting/updating.

Thanks,
Dan
0
Princy
Top achievements
Rank 2
answered on 02 Mar 2011, 12:03 PM
Hello Dan,

What issue you have faced when trying that code. If you are not able to extract the new values from the edited table row, then better option is using 'ExtractValuesFromItem' method as described in this documentation.
Inserting values in-place and EditForms
Inserting values using UserControl/FormTemplate

Thanks,
Princy.
0
Dan
Top achievements
Rank 1
answered on 02 Mar 2011, 02:20 PM
Princy,

Thanks for your reply.
When I try this:

protected void rgEmployees_InsertCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item;
            string resignationDate = (insertedItem["ResignationDate"].Controls[0] as TextBox).Text;
            if (resignationDate == "") (insertedItem["Outprocessed"].Controls[0] as TextBox).Text = "false";
            else (insertedItem["Outprocessed"].Controls[0] as TextBox).Text = "true";
        }

I get the old "NullReferenceException was unhandled by user code", Object reference not set to an instance of an object
error pointing at the "string resignationDate = (insertedItem["ResignationDate"].Controls[0] as TextBox).Text;" line.

I've seen all of your referenced examples via searches. They either don't apply to entitydatasources, or they are rebuilding the Insert/update commands from scratch. I'm hoping to be able to do simple logic before the  entitydatasource performs the insert/udpate, as you can do with an asp:gridview. 

Again, I need a very simple example that has these requirements:
1) A radgrid that's bound to an entitydatasource
2) Before inserting or updating to the database, perform simple logic such as:
if fieldA == "A" then fieldB="B";

Thanks,
Dan




0
Dan
Top achievements
Rank 1
answered on 02 Mar 2011, 03:32 PM
Princy,

I was able to do this using GetInsertItem method at the InsertCommand event. The update logic should be similiar. If anyone else is trying to do this, here's what worked for me. 

<telerik:RadGrid ID="rgEmployees" Skin="Web20" ShowGroupPanel="True" runat="server"
            DataSourceID="edsEmployees" AllowAutomaticInserts="True" AllowAutomaticUpdates="True"
            AllowFilteringByColumn="True" AutoGenerateColumns="False" OnItemCreated="rgEmployees_ItemCreated">
...
           <telerik:GridTemplateColumn UniqueName="ResignationDate" DataField="ResignationDate" HeaderText="Resignation Date">
                        <EditItemTemplate>
                            <telerik:RadDateInput ID="dtResignationDate" runat="server">
                            </telerik:RadDateInput>
                        </EditItemTemplate>
           </telerik:GridTemplateColumn>
           <telerik:GridTemplateColumn UniqueName="Outprocessed" DataField="Outprocessed" Visible="false" ReadOnly="false">
                        <EditItemTemplate>
                            <asp:TextBox ID="txtOutprocessed" runat="server" Text='<%# Bind("Outprocessed") %>'></asp:TextBox>
                        </EditItemTemplate>
           </telerik:GridTemplateColumn>
...
</telerik:RadGrid>

        
 protected void rgEmployees_InsertCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridEditableItem insertedItem = e.Item.OwnerTableView.GetInsertItem();
            GridEditFormItem editFormItem = insertedItem as GridEditFormItem;
            RadDateInput dtResignationDate = editFormItem.FindControl("dtResignationDate") as RadDateInput;
            TextBox txtOutprocessed = editFormItem.FindControl("txtOutprocessed") as TextBox;
            if (dtResignationDate.Text == "") txtOutprocessed.Text = "false";
            else txtOutprocessed.Text = "true";
        }

Dan
Tags
Grid
Asked by
Dan
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Dan
Top achievements
Rank 1
Share this question
or