Telerik RadGrid provides an API for inserting new data, updating existing data and deleting data from the specified data source. The new ASP.NET 2.0 framework allows using these features with writing very little code.
Controlling the automatic data source operations
Telerik RadGrid has to be bound to any DataSource control that supports Insert/Update/Delete. Then Telerik RadGrid can take an advantage of the data source capabilities and will perform the required operations arbitrary codeless with only the error handling code required. In order to achieve this though, the developer should set the properties related to the automatic data source operations. There are three properties that control the grid behavior for automatic Insert/Update/Delete:
- AllowAutomaticDeletes="True"
- AllowAutomaticInserts="True"
- AllowAutomaticUpdates="True"
In order to insert/update/delete items automatically (without any extra coding) in Telerik RadGrid through a DataSourceControl API you have to relate a DataSourceControl to your grid instance through the DataSourceID attribute of the grid. Keep in mind that you will have to configure the data source control (including update/insert/delete parameters to its definition) to trigger the automatic operations. Here is more info in the MSDN about ASP.NET 2.0 DataSource controls and their configurations:
AccessDataSource:
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.accessdatasource.aspx
SqlDataSource:
http://msdn2.microsoft.com/en-us/library/dz12d98w(vs.80).aspx
ObjectDataSource:
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.objectdatasource.aspx
Note that you will also need to provide proper DataKeyNames for the grid to ensure that the insert/update/delete operation will be performed as expected.
Review this online demo of the product as well to see that feature of the product in reality. You may also be interested in browsing the code library project linked below:
Automatic operations with SqlDataSource control
Automatic operations with ObjectDataSource control
Automatic operations with SqlDataSource control in hierarchical grid
Insert/Update/Delete with stored procedures and SqlDataSource control
Extracting values
Telerik RadGrid has four types of columns capable of editing data that will be persisted automatically. These are:
- GridBoundColumn
- GridCheckboxColumn
- GridDropDownColumn
- GridTemplateColumn
By default Telerik RadGrid will extract the values from the corresponding editors of the currently edited GridItem when updating or inserting a new record of all columns unless they are set as read-only. When deleting an item, Telerik RadGrid will extract the values from the cells of the GridItem which will be deleted. The extraction of all values is necessary when the datasource control has ConflictDetection="CompareAllValues" (the default DataSource control behavior).
Extracting values from an Item is supported only when the grid is InPlace or EditForms (auto-generated edit forms) or use a template.
 |
ASP.NET 2.0 does not support automatic values extraction from UserControls |
Telerik RadGrid can extract values even from columns that are set as read-only, if the column's property ForceExtractValue is set to:
- "InBrowseMode" - when deleting records
- "InEditMode" - when inserting/updating records
- "Always" - for all modes
The default value for this property is "None", i.e. the extraction of the default values will not be performed only for read-only columns.
Values from a template column will be extracted only for properties bound using the ASP.NET2.0 syntax:
In this example:
- When deleting records - Telerik RadGrid will try to extract the values for the Item cells from the ItemTemplate. As the text for the Label ID="lblUnitPrice" is bound using the Eval expression Telerik RadGrid will NOT extract a value from this Label.
- When inserting/updating records - Telerik RadGrid will extract the values using the EditItemTemplate. When it is set in edit mode, Telerik RadGrid will manage to extract the value from the ID="tbUnitPrice" text box for Update/Insert operation as it is bound using the Bind expression.
Automatic operations with WebUserControl as EditForm
Automatic operations with WebUserControl as EditForm through the DataSource control (used for grid content generation) are not supported in this version of Telerik RadGrid.
Generally, if you would like to perform automatic Update/Insert/Delete with custom edit form, you have two options:
- Use a template edit form (FormTemplate) instead of WebUserControl. You can simply copy the template from the user control to the edit form template and modify the binding logic using the Bind() syntax (pseudo two-way binding) instead of DataBinder.Eval (which is the general one-way binding):
http://www.telerik.com/demos/aspnet/Grid/Examples/DataEditing/TemplateFormUpdate/DefaultCS.aspx
(note that in this case Telerik RadGrid automatically extracts the edited values and calls the update method of the DataSourceControl)
- The second option is to implement the data-editing manually.
- In the code of the UserControl you should write code to extract the values from all controls (like textboxes and other input).
- You can use DataAdapters to save the changes to your database. You can also execute the DataSourceView.Update() method as described in the ASP.NET documentation:
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.objectdatasourceview.aspx
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.objectdatasource.update.aspx