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

Can't set grid item values on insert

5 Answers 274 Views
Grid
This is a migrated thread and some comments may be shown as answers.
tony
Top achievements
Rank 1
tony asked on 05 May 2012, 02:49 AM
i am having the worst time trying to set the value of a column of a radgrid when the user clicks the "Add New Item" link.

the setup is a RadGrid control bound to an EntityDataSource whose underlying table has two primary keys, one of which is foreign ( much like a parent child scenario). that key is ItemID.

The real problem is that though RadGrid knows the keys - it will not use them in the insert, thereby generating a foreign key constraint error. so i get to find some way to populate the grid column ItemID with the foreign key value ItemID.

the event handler is:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
GridEditableItem editedItem = e.Item as GridEditableItem;
if (editedItem != null)
{
editedItem["ItemID"].Text = editedItem.GetDataKeyValue("ItemID").ToString();
}
}

the column of interest is:
<telerik:GridBoundColumn DataField="ItemID"
    FilterControlAltText="Filter column column" HeaderText="Item ID"
    UniqueName="ItemID"  DefaultInsertValue='<%# I wish i could reference the Key Values%>' >
</telerik:GridBoundColumn>
 
unfortunately when the editor is displayed (after the user clicks the Add New Item link), the ItemID value is blank causing the insert into the table to fail when the user clicks insert. i am using default Add New functionality - whatever that is called.

what am i doing wrong?





5 Answers, 1 is accepted

Sort by
0
tony
Top achievements
Rank 1
answered on 05 May 2012, 03:23 AM
ps - i am assuming that the ItemCreated event has resulted in a new item / row being created and added to the grid pending either an insert or cancelation by the user.
0
tony
Top achievements
Rank 1
answered on 05 May 2012, 04:19 AM
this problem was rather stupid - i created a local variable for processing but did nothing to propagate its state to the grid. figuring out how to do that will keep me busy.
0
tony
Top achievements
Rank 1
answered on 05 May 2012, 07:26 AM
it seems that the cleanest solution is to change the event to InsertCommand and use code resembling the following:

now that i can set the foreign key value, my insert succeeds. the radgrid is a maze and house of mirrors. but it is better than the vs gridview.
protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
 
{
             GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item.OwnerTableView.GetInsertItem();    
             TextBox ItemIDTextBox = (insertedItem["ItemID"].Controls[0] as TextBox);
             ItemIDTextBox.Text = "some value";
}
0
Daniel
Top achievements
Rank 1
answered on 10 Sep 2012, 02:10 PM
Many thanks for posting your findings, I asked the same question and didn't get a reply either. It seems to be something that should be very easy. 

This is the code I derived from yours. Where Metrics is the GridTableView Name and ID the name of the parents Primary Key.

protected void radgridReports_InsertCommand(object sender, GridCommandEventArgs e)
    {
        GridDataItem dataItem = (GridDataItem)e.Item.OwnerTableView.ParentItem;
        if (dataItem != null)
        {
            if (e.Item.OwnerTableView.Name == "Metrics")
            {
                string ReportId = dataItem.GetDataKeyValue("ID").ToString();
 
                //For Edit Form mode
                //GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item.OwnerTableView.GetInsertItem();
 
                // For Inplace mode
                GridDataInsertItem insertedItem = (GridDataInsertItem)e.Item.OwnerTableView.GetInsertItem();
                 
                TextBox ForgienKeyTextBox = (insertedItem["ReportId"].Controls[0] as TextBox);
                ForgienKeyTextBox.Text = ReportId;
            }
        }
    }
0
Tsvetoslav
Telerik team
answered on 13 Sep 2012, 12:32 PM
Hello Daniel,

How to set default values for the insert fields is explained in the following help article (the paragraph titled: Setting predefined values for different column editors):
http://www.telerik.com/help/aspnet-ajax/grid-inserting-values-inplace-and-editforms.html

Hope it helps.

Greetings,
Tsvetoslav
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
tony
Top achievements
Rank 1
Answers by
tony
Top achievements
Rank 1
Daniel
Top achievements
Rank 1
Tsvetoslav
Telerik team
Share this question
or