Fixing the ‘Telerik.WebControls.GridInsertionObject does not contain a property’ Error

I really like working with the Telerik ASP.NET AJAX controls. However, I keep forgetting about a problem in the RadGrid that occurs when trying to add a new record using EntityDataSource. If there aren’t any records yet, and you click Add Record, you often get this:

DataBinding: 'Telerik.Web.UI.GridInsertionObject' does not contain a property with the name ‘<fieldname>'. 

The workaround is to manually initialize the object being inserted. Here’s the routine (VB) for my future reference and yours:

Protected Sub RadGrid1_ItemCommand _
(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) _
    Handles RadGrid1.ItemCommand
     ' For error 'Telerik.WebControls.GridInsertionObject' does not contain a property
     ' Handles the case where you are adding a new item to the grid where
     ' none exists yet. The purpose is to create a dummy collection of values
     ' to prefill the form
     If e.CommandName = RadGrid.InitInsertCommandName Then
         e.Canceled = True
         Dim newValues As System.Collections.Specialized.ListDictionary = _
         New System.Collections.Specialized.ListDictionary()
         newValues("Note") = ""
         newValues("DisplayOrder") = 0
         newValues("ID") = 0
         'Insert the item and rebind
     End If
End Sub

Full Disclosure: Telerik gave me a copy of their controls as a perk for being a Microsoft MVP for ASP.NET.



Comments have been disabled for this content.