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

Accessing a custom object bound to the row being updated in UpdateCommand

4 Answers 107 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Makoto
Top achievements
Rank 1
Makoto asked on 05 Sep 2012, 03:38 AM

Hello,

I have a RadGrid that binds to a collection of custom object. In my application users can insert/update the rows of the grid and the changes will not be persisted to the database until they click on a save button. Each row has a key column containing the key value (ProviderLanguageID) assigned to each of the object when data is persisted to DB.

The grid's markup is like this:

<telerik:RadGrid ID="grdProviderLanguage" runat="server"
    AutoGenerateColumns="False" Height="140px" Width="360px"
    OnNeedDataSource="grdProviderLanguage_NeedDataSource"
    oninsertcommand="grdProviderLanguage_InsertCommand"
    onitemcommand="grdProviderLanguage_ItemCommand"
    CellSpacing="0" GridLines="None"
    onupdatecommand="grdProviderLanguage_UpdateCommand"
    onitemdatabound="grdProviderLanguage_ItemDataBound">
    <ClientSettings>
        <Selecting AllowRowSelect="True" />
        <KeyboardNavigationSettings AllowActiveRowCycle="True" />
        <Scrolling AllowScroll="True" UseStaticHeaders="True" />
    </ClientSettings>
    <MasterTableView CommandItemDisplay="Top" InsertItemDisplay="Top" EditMode="InPlace" EnableNoRecordsTemplate="false" DataKeyNames="ProviderLanguageID">
        <CommandItemSettings AddNewRecordImageUrl="Images/AddNew.png" AddNewRecordText="" ShowRefreshButton="false"></CommandItemSettings>
        <RowIndicatorColumn Visible="False" FilterControlAltText="Filter RowIndicator column">
            <HeaderStyle Width="20px"></HeaderStyle>
        </RowIndicatorColumn>
        <ExpandCollapseColumn Visible="False" FilterControlAltText="Filter ExpandColumn column">
            <HeaderStyle Width="20px"></HeaderStyle>
        </ExpandCollapseColumn>
        <EditFormSettings>
        <EditColumn FilterControlAltText="Filter EditCommandColumn column"></EditColumn>
        </EditFormSettings>
        <Columns>
            <telerik:GridTemplateColumn HeaderText="Language" UniqueName="ProviderLanguage" DataField="ProviderLanguageID">
                <HeaderStyle Width="170px" />
                <ItemTemplate>
                    <asp:Label ID="lblLanguageName" runat="server" Text='<%# Eval("LanguageObject.LanguageName") %>' />
                </ItemTemplate>
                <EditItemTemplate>
                    <telerik:RadComboBox ID="cmbProviderLanguage" runat="server" width="130px"
                        EnableLoadOnDemand="true" DataValueField="LanguageID" DataTextField="LAnguageObject.LanguageName"
                        OnItemsRequested="cmbProviderLanguage_ItemsRequested"
                        Text='<%# Eval("LanguageObject.LanguageName") %>'>
                    </telerik:RadComboBox>                                                                                                           
                </EditItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridCheckBoxColumn HeaderText="Primary" UniqueName="IsPrimary" DataField="IsPrimary" />
            <telerik:GridEditCommandColumn ButtonType="LinkButton" InsertText="Done" EditText="Edit" CancelText="Cancel" UpdateText="Done" />
        </Columns>
    </MasterTableView>
    <FilterMenu EnableImageSprites="False"></FilterMenu>
</telerik:RadGrid>

When multiple rows are inserted to the grid, and one of the rows is subsequently edited (before saved to DB), the edited row is accessible in UpdateCommand event like this:

Guid editedItemID = new Guid(editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["ProviderLanguageID"].ToString());

I'm using the key value to locate the actual custom object in the collection stored in the page.

This works if each of the record already has a key. When a new record is created, the key value is empty (Guid.Empty). So we'd have to use different means to locate the custom object. I'm thinking of comparing the SavedOldValues with the saved values to locate the actual object.

I notcied that e.Item.DataItem is null in UpdateCommand event, but not in ItemDataBound event. Is there any way we can access the custom object the row being updated is bound to?

Thanks in advance,
Makoto

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 05 Sep 2012, 04:51 AM
Hi,

I understood that you are updating the data using DataKeyValue and it is not set for the newly inserted value. One suggestion is to give
an option to insert the DataKeyValue while inserting new data.
aspx:
<telerik:GridBoundColumn Display="false"  DataField="ProviderLanguageID" UniqueName="ProviderLanguageID" HeaderText="ProviderLanguageID"></telerik:GridBoundColumn>
C#:
protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
{
   GridEditFormInsertItem item = (GridEditFormInsertItem)e.Item;
   TextBox txt = (TextBox)item["ProviderLanguageID"].Controls[0];
   //code to insert
}
protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
 if (e.Item is GridEditableItem && e.Item.IsInEditMode && !(e.Item is GridEditFormInsertItem))
 {
   GridEditableItem item = (GridEditableItem)e.Item;
   TextBox txtbox = (TextBox)item["ProviderLanguageID"].Controls[0] as TextBox;//showing the column in insert mode only
   txtbox.Parent.Parent.Visible = false;
 }
}

Thanks,
Shinu.
0
Makoto
Top achievements
Rank 1
answered on 05 Sep 2012, 05:57 PM
Thanks Shinu. Unfortunately I don't have access to the actual DataKeyValue until the records are persisted to the DB (which occurs when user press a separate "Save" button on the UI). I'm reading other post about e.Item.DataItem being empty in events other than ItenDataBind - sounds like I'd have to look for the actual object by value comparison.

I appreciate your quick suggestion, though :)
0
Accepted
Shinu
Top achievements
Rank 2
answered on 06 Sep 2012, 04:15 AM
Hi Makoto,

You can retrieve the old values for the edited item through the SavedOldValues property of the GridEditableItem. Here is the sample code I tried.

C#:
protected void grdProviderLanguage_UpdateCommand(object sender, GridCommandEventArgs e)
{
    GridEditableItem eitem = (GridEditableItem)e.Item;
    Hashtable Hashtable1 = new Hashtable();
    Hashtable1 = (Hashtable)eitem.SavedOldValues;
    string oldValue = Hashtable1["cmbProviderLanguage"].ToString(); //you will get the old values here
    RadComboBox Combo = (RadComboBox)eitem.FindControl("cmbProviderLanguage");
    string ProviderLanguage = Combo.SelectedItem.Text; //new value
 
    //your code for updation
     
}

Thanks,
Shinu.
0
Makoto
Top achievements
Rank 1
answered on 06 Sep 2012, 08:52 PM
Yes, SavedOldValues will have old values if column was bound column, etc.. But it doesn't seem to do that for template columns.

I worked around it by storing the old object's value in session in ItemDataBound event and retrieving it in UpdateCommand event, as detailed in this article:
http://www.telerik.com/help/aspnet-ajax/grid-retrieve-original-values-for-edited-item.html

Thanks Shinu though - you've been very helpful!
Makoto
Tags
Grid
Asked by
Makoto
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Makoto
Top achievements
Rank 1
Share this question
or