RadGrid for ASP.NET

Edit forms Send comments on this topic.
Insert/Update/Delete records > Edit mode > Edit forms

Glossary Item Box

To display the grid column editors in auto-generated form when switching grid item in edit mode (see the screenshot below), you simply need to change the MasterTableView EditMode property to EditForms.

ASPX/ASCX Copy Code
<rad:RadGrid id="RadGrid1" runat="server">
<MasterTableView AutoGenerateColumns="True" EditMode="EditForms" />
</
rad:RadGrid>

 

 

Since version 4.0.3 Telerik RadGrid exposes MaxLength property for GridBoundColumn. Setting this property to integer value will limit the number of symbols which can be entered in the default textbox editor (when item is switched in edit mode).

Relations between the edited item and the item in regular mode

When EditForms editing is applied, the grid row is of type GridDataItem in regular mode and  GridEditableItem in edit regime. Hence you can cast the item in the ItemCreated event (for example) to those types according to its current mode:


C# Copy Code
private void RadGrid1_ItemCreated(object sender, Telerik.WebControls.GridItemEventArgs e)
{
  
if(e.Item is GridDataItem)
  {
     
//the item is in regular mode
     
GridDataItem dataItem = e.Item as GridDataItem;
     
//do something here
   }
  
else if (e.Item is GridEditableItem && e.Item.IsInEditMode)
  {
    
//the item is in edit mode
    
GridEditableItem editedItem = e.Item as GridEditableItem;
    
//do something here
  }

}

VB.NET Copy Code
Private Sub RadGrid1_ItemCreated(ByVal sender As Object, ByVal e As Telerik.WebControls.GridItemEventArgs) Handles RadGrid1.ItemCreated
         If (TypeOf e.Item is GridDataItem) Then

             'the item is in regular mode
             Dim dataItem As GridDataItem = CType(e.Item,GridDataItem)
             'do something here

         ElseIf (TypeOf e.Item is GridEditableItem AndAlso e.Item.IsInEditMode) Then

             'the item is in edit mode
             Dim editedItem As GridEditableItem = CType(e.Item, GridEditableItem)
             'do something here

         End If
End Sub



Additionally, when the item is edited you can cast it to GridEditFormItem (class which extends the GridEditableItem class) in order to refer to the controls in it (through the EditFormCell property of the GridEditFormItem) or to the parent data item object (through the ParentItem property). This, however, can be done inside UpdateCommand/InsertCommand/ItemCommand handler (if needed):

C# Copy Code
if(e.Item is GridDataItem)
{
   
//find the edit form item
   
GridEditFormItem formItem = (e.Item as GridDataItem).EditFormItem as GridEditFormItem;

  
//get the cell which wraps the controls inside the edit form
   
TableCell cell = formItem.EditFormCell as TableCell;
}
if (e.Item is GridEditableItem)
{
    
//cast the item to GridEditFormItem
    
GridEditFormItem formItem = e.Item as GridEditFormItem;

    
//reference its parent data item
    
GridDataItem dataItem = formItem.ParentItem as GridDataItem;
}

 

VB.NET Copy Code
If (TypeOf e.Item is GridDataItem) Then

    'find the edit form item
    Dim formItem As GridEditFormItem = CType(CType(e.Item,GridDataItem).EditFormItem,GridEditFormItem)

    'get the cell which wraps the controls inside the edit form
    Dim cell As TableCell = CType(formItem.EditFormCell,TableCell)
End If

If (TypeOf e.Item is GridEditableItem) Then

    'cast the item to GridEditFormItem
    Dim formItem As GridEditFormItem = CType(e.Item,GridEditFormItem)

    'reference its parent data item
    Dim dataItem As GridDataItem = CType(formItem.ParentItem,GridDataItem)
End If


Further information about how to reference controls inside grid rows/edit form you can find in this topic.


 

The EditItems collection contains InPlace edit mode items. When you switch the edit type to EditForms, the EditItems collection holds the currently edited items but not their EditFormItems (which in this case hold the new values). If you would like your application to support both edit regimes when traversing the EditItems collection, you have to make the following modification inside the loop:

C# Copy Code
foreach (GridDataItem item in RadGrid1.EditItems)
   {
    GridEditableItem itemToEdit =
    (item.OwnerTableView.EditMode == GridEditMode.InPlace)?
    (GridEditableItem)item : (GridEditableItem)item.EditFormItem;
    
//perform further operations
}
VB.NET Copy Code
Dim item As GridDataItem

For Each item In RadGrid1.EditItems
      Dim itemToEdit As GridEditableItem = (IIf item.OwnerTableView.EditMode = GridEditMode.InPlace Then CType(item, GridEditableItem) Else CType(item.EditFormItem, GridEditableItem))
  'perform further operations
Next item