RadGrid for ASP.NET

Retrieving original values for edited item Send comments on this topic.
Insert/Update/Delete records > Retrieving original values for edited item

Glossary Item Box

You can retrieve the old values for the edited item through the SavedOldValues property of the GridEditableItem (which is IDictionary collection of key -> value pairs). The keys in this collection are the UniqueNames of the editable columns and the values are the cell content for the edited row before the edit operation.
The new values which the user entered before triggering the Update command can be fetched calling the ExtractValuesFromItem method.

Here is a sample code (note that this approach is applicable for auto-generated grid column editors):

ASPX/ASCX Copy Code
<rad:RadGrid ID="RadGrid1" DataSourceID="AccessDataSource1" AllowSorting="True"
runat="server" OnItemCommand="RadGrid1_ItemCommand">
<MasterTableView DataSourceID="AccessDataSource1" AllowAutomaticUpdates="True">
               
<Columns>
                   
<rad:GridEditCommandColumn UniqueName="EditCommandColumn">
                   
</rad:GridEditCommandColumn>
               
</Columns>
</MasterTableView>
</
rad:RadGrid>
<br />
<
asp:AccessDataSource ID="AccessDataSource1" DataFile="~/Grid/Data/Access/Nwind.mdb"
SelectCommand="SELECT TOP 10 ContactName, ContactTitle, Address FROM Customers"
runat="server"></asp:AccessDataSource>

And in the code-behind:

C# Copy Code
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
{
        
if (e.CommandName == RadGrid.UpdateCommandName)
       {
            
if (e.Item is GridEditableItem)
           {
               GridEditableItem editedItem = e.Item
as GridEditableItem;
               
//here editedItem.SavedOldValues will be the dictionary which holds the
               
//predefined values

              
//Prepare new dictionary object
               
Hashtable newValues = new Hashtable();
               e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
              
//the newValues instance is the new collection of key -> value pairs
              
//with the updated ny the user data

           }
       }
}
VB.NET Copy Code
Protected Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As GridCommandEventArgs)
 If (e.CommandName = RadGrid.UpdateCommandName) Then
  If (e.Item = GridEditableItem) Then
   Dim editedItem As GridEditableItem = CType(e.Item,GridEditableItem)
   'here editedItem.SavedOldValues will be the dictionary which holds the
   'predefined values
   'Prepare new dictionary object
   Dim newValues As Hashtable = New Hashtable
   e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem)
   'the newValues instance is the new collection of key -> value pairs
   'with the updated ny the user data
  End If
 End If
End Sub

 

If you have GridTemplateColumn inside the grid and want to obtain its original values on update command, you will need to store the old value on ItemDataBound when the grid item is in edit mode and then reference it on update. The code snippet below uses a Session variable for this purpose:

ASPX/ASCX Copy Code
<rad:RadGrid ID="RadGrid1" DataSourceID="AccessDataSource1" Skin="WinXP" runat="server" Width="150px">
               
<MasterTableView AutoGenerateColumns="false">
                   
<Columns>
                       
<rad:GridTemplateColumn UniqueName="TemplateColumn" HeaderText="City">
                           
<ItemTemplate>
                               
<%# Eval("City") %>
                           
</ItemTemplate>
                           
<EditItemTemplate>
                               
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("City") %>' />
                           
</EditItemTemplate>
                       
</rad:GridTemplateColumn>
                       
<rad:GridEditCommandColumn UniqueName="EditCommandColumn" />
                   
</Columns>
               
</MasterTableView>
</
rad:RadGrid>
<
br />
<
asp:AccessDataSource ID="AccessDataSource1" DataFile="~/Grid/Data/Access/Nwind.mdb"
 
SelectCommand="SELECT TOP 10 City FROM Customers" runat="server"></asp:AccessDataSource>

 

C# Copy Code
protected void RadGrid1_UpdateCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
{
       RadGrid1.Controls.Add(
new LiteralControl("Saved old value for the City editor is: " & (string)Session["savedOldValue"]));
}
protected void RadGrid1_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)
{
       
if (e.Item is GridEditableItem && e.Item.IsInEditMode)
       {
           TextBox txtBox = e.Item.FindControl(
"TextBox1") as TextBox;
           Session[
"savedOldValue"] = txtBox.Text;
       }
}

 

VB.NET Copy Code
Protected Sub RadGrid1_UpdateCommand(ByVal source As Object, ByVal e As Telerik.WebControls.GridCommandEventArgs) Handles RadGrid1.UpdateCommand
        RadGrid1.Controls.Add(New LiteralControl("Saved old value for the City editor is: " & CType(Session("savedOldValue"), String)))
End Sub
Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.WebControls.GridItemEventArgs) Handles RadGrid1.ItemDataBound
        If (TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode) Then
            Dim txtBox As TextBox = CType(e.Item.FindControl("TextBox1"), TextBox)
            Session("savedOldValue") = txtBox.Text
        End If
End Sub