This is a migrated thread and some comments may be shown as answers.

Multi Edit batches and paging does not work

1 Answer 74 Views
Grid
This is a migrated thread and some comments may be shown as answers.
mac
Top achievements
Rank 1
mac asked on 23 Feb 2009, 12:55 AM
hi. going through the white papers trying to figure out how to get my grid to load as edit mode, allow me update several rows, and then select one button to update them all. I went through a combination of Batch update, but that one is silly as it still wants you to select the rows you want to edit, (http://www.telerik.com/help/aspnet-ajax/grdperformingbatchupdates.html) and  default edti mode on load (http://www.telerik.com/help/aspnet/grid/grddefaulteditmodeforgriditemsoninitialload.html) which is close but still wants you to click update on every row. I managed to get the grid to load into edit mode when paging but cannot get the itemcommand to recognize new data after the first page. The first page updates exactly as expected. My requirements are to stay load into and stay in edit mode, page through items and update on other pages all with the same single button.
Code  and Souce
Protected Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs)  
        If (e.CommandName = "UpdateAll") Then  
            For 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)  
                If newValues("Balance") IsNot Nothing Then  'WITHOUT THIS THE WHERE CLAUSE FAILS EACH TIME ON NEW PAGES.
                    SqlDataSource1.UpdateCommand = String.Format("Update Account SET Balance= {0} WHERE ID= {1} ", newValues("Balance"), editedItem.GetDataKeyValue("ID").ToString())  
                    SqlDataSource1.Update()  
                End If  
                editedItem.Edit = True 
            Next  
        End If  
 
        multiEdit()  
        RadGrid1.Rebind()  
    End Sub  
      
    Protected Sub RadGrid1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadGrid1.DataBound  
        If IsPostBack Then  
            multiEdit()  
        End If  
    End Sub  
      
    Protected Sub RadGrid1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadGrid1.PreRender  
        If Not IsPostBack Then  
            multiEdit()  
            RadGrid1.Rebind()  
        End If  
 
    End Sub  
 
    Sub multiEdit()  
 
        For Each item As GridItem In RadGrid1.MasterTableView.Items  
            If TypeOf item Is GridEditableItem Then  
                Dim editableItem As GridEditableItem = CType(item, GridDataItem)  
                editableItem.Edit = True 
            End If  
        Next  
 
    End Sub 

Source
 
<asp:SqlDataSource ID="SqlDataSource1" runat="server"   
ConnectionString="<%$ ConnectionStrings:PHCJobPostsConn %>"   
SelectCommand="SELECT * FROM [Account]"   
</asp:SqlDataSource> 
<telerik:RadGrid ID="RadGrid1" runat="server" AllowMultiRowEdit="True" DataSourceID="SqlDataSource1" 
               OnItemCommand="RadGrid1_ItemCommand" OnItemDataBound="RadGrid1_ItemDataBound">  
               <MasterTableView   DataKeyNames="ID" AutoGenerateColumns="false" EditMode="InPlace" 
                   CommandItemDisplay="TopAndBottom">  
                <Columns> 
              
                    <telerik:GridBoundColumn ReadOnly="true" DataField="ID" DataType="System.Int32" HeaderText="ID"  SortExpression="ID" UniqueName="ID">  
            </telerik:GridBoundColumn> 
            <telerik:GridBoundColumn ReadOnly="true" DataField="Name" HeaderText="Name" SortExpression="Name" UniqueName="Name">  
            </telerik:GridBoundColumn> 
            <telerik:GridBoundColumn ReadOnly="true" DataField="AccountNo" HeaderText="AccountNo" SortExpression="AccountNo" UniqueName="AccountNo">  
            </telerik:GridBoundColumn> 
                <telerik:GridTemplateColumn> 
            <ItemTemplate> 
            <%#Eval("Balance")%> 
            </ItemTemplate> 
            <EditItemTemplate> 
            <asp:TextBox CssClass="none" ID="BalanceTB" Text='<%#Bind("Balance") %>' runat="server" /> 
            </EditItemTemplate> 
            </telerik:GridTemplateColumn> 
          
            <telerik:GridBoundColumn ReadOnly="true" DataField="InsertDate" DataType="System.DateTime" HeaderText="InsertDate" SortExpression="InsertDate" UniqueName="InsertDate">  
            </telerik:GridBoundColumn> 
              
    </Columns> 
 <CommandItemTemplate> 
    <asp:Button runat="server" ID="UpdateAll" Text="Update All" CommandName="UpdateAll" /> 
</CommandItemTemplate> 
               </MasterTableView> 
</telerik:RadGrid> 


1 Answer, 1 is accepted

Sort by
0
Yavor
Telerik team
answered on 25 Feb 2009, 10:47 AM

Hi mac,

You can use the second approach to preload the control in edit mode. Then, you can update all the items, as shown in the first article. This is handled by this code:

.cs

protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)  
   {  
       if (e.CommandName == "UpdateAll")  
       {  
           foreach (GridEditableItem editedItem in RadGrid1.EditItems)  
           {                 
               Hashtable newnewValues = 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 CustomerIDCustomerID='{1}'", newValues["ContactName"], editedItem.GetDataKeyValue("CustomerID").ToString());  
               SqlDataSource1.Update();  
               editedItem.Edit = false;  
           }  
       }  
       RadGrid1.Rebind();  
   } 

As shown, this iterates through the editItems collection of the grid, to proceed with the updates. However, this is only limited to the current page. On all other pages, the control reloads its data, to fetch new records. Nevertheless, you can simply save the data, when leaving the current grid page.
I hope this suggestion helps.

Regards,

Yavor
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
Grid
Asked by
mac
Top achievements
Rank 1
Answers by
Yavor
Telerik team
Share this question
or