A common scenario is to force several grid items in edit mode and then update them on single button click.
In the example below there is UpdateAll button in the grid command item template. The visitor can edit several grid items by pressing the edit button for each item(the Update button will be hidden and only the Cancel button will be present in edit mode). After editing the content of the grid items, a batch update is triggered when hitting the UpdateAll button in the command item template (the update is executed in the ItemCommand event handler when e.CommandName = UpdateAll). The idea is to iterate through all items in the EditItems collection of the grid and update their data with the new values in the grid data source.
ASP.NET
<telerik:RadGridRenderMode="Lightweight"ID="RadGrid1"runat="server"AllowMultiRowEdit="True"DataSourceID="SqlDataSource1"OnItemCommand="RadGrid1_ItemCommand"OnItemDataBound="RadGrid1_ItemDataBound"><MasterTableViewDataKeyNames="CustomerID"AutoGenerateColumns="false"EditMode="InPlace"CommandItemDisplay="TopAndBottom"><Columns><telerik:GridBoundColumnReadOnly="true"DataField="CustomerID"UniqueName="CustomerID"HeaderText="CustomerID"></telerik:GridBoundColumn><telerik:GridBoundColumnDataField="ContactName"UniqueName="ContactName"HeaderText="ContactName"></telerik:GridBoundColumn><telerik:GridEditCommandColumnUniqueName="EditCommandColumn"/></Columns><CommandItemTemplate><asp:Buttonrunat="server"ID="UpdateAll"Text="Update All"CommandName="UpdateAll"/></CommandItemTemplate></MasterTableView></telerik:RadGrid><asp:SqlDataSourceID="SqlDataSource1"ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"ProviderName="System.Data.SqlClient"SelectCommand="SELECT TOP 10 CustomerID, ContactName FROM Customers"runat="server"></asp:SqlDataSource>
C#
protectedvoidRadGrid1_ItemCommand(object source,Telerik.Web.UI.GridCommandEventArgs e){if(e.CommandName =="UpdateAll"){foreach(GridEditableItem editedItem in RadGrid1.EditItems){Hashtable newValues =newHashtable();//The GridTableView will fill the values from all editable columns in the hash
e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
SqlDataSource1.UpdateCommand = String.Format("Update Customers SET ContactName='{0}' WHERE CustomerID='{1}'", newValues["ContactName"], editedItem.GetDataKeyValue("CustomerID").ToString());
SqlDataSource1.Update();
editedItem.Edit =false;}}
RadGrid1.Rebind();}protectedvoidRadGrid1_ItemDataBound(object sender,Telerik.Web.UI.GridItemEventArgs e){if(e.Item isGridDataItem&& e.Item.IsInEditMode){GridDataItem dataItem = e.Item asGridDataItem;//Hides the Update button for each edit form
dataItem["EditCommandColumn"].Controls[0].Visible =false;}}
VB
ProtectedSub RadGrid1_ItemCommand(ByVal source AsObject,ByVal e As Telerik.Web.UI.GridCommandEventArgs)If(e.CommandName ="UpdateAll")ThenFor Each editedItem As GridEditableItem In RadGrid1.EditItems
Dim newValues As Hashtable =New Hashtable
'The GridTableView will fill the values from all editable columns in the hash
e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem)
SqlDataSource1.UpdateCommand =String.Format("Update Customers SET ContactName='{0}' WHERE CustomerID='{1}'", newValues("ContactName"), editedItem.GetDataKeyValue("CustomerID").ToString())
SqlDataSource1.Update()
editedItem.Edit =FalseNextEndIf
RadGrid1.Rebind()EndSubProtectedSub RadGrid1_ItemDataBound(ByVal sender AsObject,ByVal e As Telerik.Web.UI.GridItemEventArgs)If(TypeOf e.Item Is GridDataItem AndAlso e.Item.IsInEditMode)ThenDim dataItem As GridDataItem =CType(e.Item, GridDataItem)'Hides the Update button for each edit form
dataItem("EditCommandColumn").Controls(0).Visible =FalseEndIfEndSub
Client-side editing with batch updateOne possible implementation is demonstrated on this demo of RadGrid for ASP.NET AJAX (review the code implementation for details). The general idea is to:
Wire the ondblclick event of the grid cells
Togglethe visibility of the column editors and store the end user changes
Push the altered content on the server and update the data source
Furthermore, a user-friendly confirm dialog will be displayed to prompt whether the updates should be propagated or discarded when you perform sorting, paging, etc. operation. The example uses RadAjaxManager instance to ajaxify the grid and perform RadAjaxManagerClientObject.ajaxRequest(args) calls. These calls are invoked in the ProcessChanges and CancelChanges client methods and the data in the source is refreshed inside the OnAjaxRequest handler on the server. The sample also features RadInputManager and RadFormDecorator which are used to filter the numeric entries and style the textboxes, checkboxes and dropdowns.
Another approach is presented in the paragraph below.
The following sample takes advantage of the editSelected() client side method, to put the control in edit mode for all items that were previously selected. Once the control is in edit mode, the user can enter data in the edit fields, and then after clicking a button in the command template, all the data will be updated from the code-behind section.
privatevoidInitializeComponent(){this.RadGrid1.NeedDataSource +=newTelerik.Web.UI.GridNeedDataSourceEventHandler(this.RadGrid1_NeedDataSource);this.RadGrid1.ItemCommand +=newTelerik.Web.UI.GridCommandEventHandler(this.RadGrid1_ItemCommand);this.Load +=newSystem.EventHandler(this.Page_Load);}publicDataTable CustomersTable
{get{DataTable res =(DataTable)this.Session["CustomersTable"];if(res ==null){
res = DataSourceHelperCS.GetDataTable("SELECT TOP 5 [CustomerID], [CompanyName], [ContactName], [ContactTitle], [Address], [City] FROM [Customers]");this.Session["CustomersTable"]= res;}return res;}}privatevoidRadGrid1_NeedDataSource(object source,Telerik.Web.UI.GridNeedDataSourceEventArgs e){
RadGrid1.DataSource =this.CustomersTable;}protectedvoidRadGrid1_ItemCommand(object source,Telerik.Web.UI.GridCommandEventArgs e){if(e.CommandName =="UpdateChanges"){if(RadGrid1.EditIndexes.Count ==0){return;}foreach(GridDataItem item in RadGrid1.EditItems){this.UpdateItem(item.EditFormItem);}
e.Item.OwnerTableView.Rebind();return;}}privatevoidUpdateItem(GridEditableItem editedItem){DataTable customersTable =this.CustomersTable;//Locate the changed row in the DataSourceDataRow[] changedRows = customersTable.Select("CustomerID = '"+ editedItem.GetDataKeyValue("CustomerID").ToString()+"'");if(changedRows.Length !=1){return;}//Update new valuesHashtable newValues =newHashtable();//The GridTableView will fill the values from all editable columns in the hash
editedItem.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);DataRow changedRow = changedRows[0];
changedRow.BeginEdit();try{foreach(DictionaryEntry entry in newValues){
changedRow[(string)entry.Key]= entry.Value;}
changedRow.EndEdit();}catch(Exception){
changedRow.CancelEdit();}}
VB
PublicReadOnlyProperty CustomersTable As DataTable
GetDim res As DataTable =CType(Me.Session("CustomersTable"), DataTable)If res IsNothingThen
res = DataSourceHelperVB.GetDataTable("SELECT TOP 5 [CustomerID], [CompanyName], [ContactName], [ContactTitle], [Address], [City", FROM(Customers),")", this.Session[CustomersTableUnknown=res)EndIfReturn res
EndGetEndPropertyPrivateSub InitializeComponent()AddHandler RadGrid1.NeedDataSource,AddressOfMe.RadGrid1_NeedDataSource
AddHandler RadGrid1.ItemCommand,AddressOfMe.RadGrid1_ItemCommand
AddHandler Load,AddressOfMe.Page_Load
EndSubPrivateSub RadGrid1_NeedDataSource(ByVal source AsObject,ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs)
RadGrid1.DataSource =Me.CustomersTable
EndSubProtectedSub RadGrid1_ItemCommand(ByVal source AsObject,ByVal e As Telerik.Web.UI.GridCommandEventArgs)If(e.CommandName ="UpdateChanges")ThenIf(RadGrid1.EditIndexes.Count =0)ThenReturnEndIfFor Each item As GridDataItem In RadGrid1.EditItems
Me.UpdateItem(item.EditFormItem)Next
e.Item.OwnerTableView.Rebind()ReturnEndIfEndSubPrivateSub UpdateItem(ByVal editedItem As GridEditableItem)Dim customersTable As DataTable =Me.CustomersTable
'Locate the changed row in the DataSourceDim changedRows()As DataRow = customersTable.Select(("CustomerID = '" _
+(editedItem.GetDataKeyValue("CustomerID").ToString +"'")))If(changedRows.Length<>1)ThenReturnEndIf'Update new valuesDim newValues As Hashtable =New Hashtable
'The GridTableView will fill the values from all editable columns in the hash
editedItem.OwnerTableView.ExtractValuesFromItem(newValues, editedItem)Dim changedRow As DataRow = changedRows(0)
changedRow.BeginEdit()TryFor Each entry As DictionaryEntry In newValues
changedRow(CType(entry.Key,String))= entry.Value
Next
changedRow.EndEdit()Catch Ex As Exception
changedRow.CancelEdit()EndTryEndSub