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

Multi-Row Edit of Detail Table

3 Answers 239 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Oleg Fridman
Top achievements
Rank 2
Oleg Fridman asked on 02 Feb 2009, 11:42 PM
I am having trouble extracting the data of a multi-row edit using the techniques described in the "Performing batch updates" article when the data is in a Detail Table, not in the main table.

To set the scene, I have two command buttons, one to Edit All Rows which loops through evey editable item of the DataTable and sets Edit=true. The other button then issues a SaveAll command to save the new data in each item.

All of my columns are GridTemplateColumns and have labels in their ItemTemplate to display the data and textbox or or other controls in their EditItemTemplate to allow for editing of the data. I am able to pull the data when the Update button is hit on the row but when I have an Update All command button, it does not work. I have the following IF statement in my ItemCommand:

if (e.CommandName == "SaveAll")  
{  
    //foreach (GridItem item in e.Item.OwnerTableView.Items)  
    foreach (Telerik.Web.UI.GridEditableItem item in rgOptions.EditItems)  
    {  
        string price = ((TextBox)e.Item.FindControl("txtPrice")).Text;  
 
        // Perform some kind of save to DB here  
 
        item.Edit = false;  
    }  
 
    e.Item.OwnerTableView.Rebind();  

The trouble is the FindControl always returns null even though I just entered data into the text box. The Item Template of the GridTemplateColumn puts the data into a label called lblPrice. This label is fully accessable via FindControl. It doesn't seem to recognize that it is in Edit Mode even though Edit = true (although IsEditMode = false, is this supposed to be the case?).

If I try the same exact code on the main table, it works great.

Any thoughts as to what I am missing?

Thank you,
Oleg Fridman

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 03 Feb 2009, 06:49 AM
Hello Oleg,

I hope you have the EditAll and SaveAll buttons placed in the CommandItemTemplate of the DetailTable. If tht is the case, you can try using the Name property of the OwnerTableView as shown in the code below, to access the textbox in the detail table:
aspx:
<telerik:RadGrid  ID="RadGrid1" runat="server" OnItemCommand="RadGrid1_ItemCommand" > 
        <MasterTableView  CommandItemDisplay="Top" EditMode="InPlace" Name="Master"
       <DetailTables> 
        <telerik:GridTableView EditMode="InPlace" DataSourceID="SqlDataSource1" CommandItemDisplay="Top" Name="Detail" runat="server" > 
        <CommandItemTemplate> 
           <asp:Button ID="Button1" runat="server" CommandName="EditAll" Text="Edit All" /> 
           <asp:Button ID="Button2" runat="server" CommandName="SaveAll" Text="Save All" /> 
       </CommandItemTemplate>        
        <Columns> 
         ... 
        </Columns> 
        </telerik:GridTableView> 
        </DetailTables> 
          ....

cs:
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) 
    { 
       if (e.CommandName == "SaveAll") 
        { 
            if (e.Item.OwnerTableView.Name == "Detail") 
            { 
                foreach (GridEditableItem item in e.Item.OwnerTableView.GetItems(GridItemType.EditItem)) 
                {                   
                        string price = ((TextBox)item.FindControl("TextBox1")).Text;                   
 
                    item.Edit = false
                } 
 
                e.Item.OwnerTableView.Rebind(); 
            } 
        }  
    } 

Thanks
Princy.
0
Oleg Fridman
Top achievements
Rank 2
answered on 03 Feb 2009, 02:14 PM
Princy,

Thanks for your reply.

I did indeed put them in the CommandItemTemplate.

Also, checking the table name is a good idea but only in the case where there are rows being edited from multiple tiers of the hierarchy.

Unfortunately, I am still right where I started.

Oleg
0
Princy
Top achievements
Rank 2
answered on 04 Feb 2009, 07:07 AM
Hello Oleg,

It seems you are placing the EditAll and SaveAll buttons in the CommandItemTemplate of the MaterTable of your grid. And on clicking the SaveAll button, I suppose you want to access the textbox for each row in the DetailTable of your grid when in EditMode. If thats the case you can, try out the following code:
aspx:
<telerik:RadGrid  ID="RadGrid1" runat="server" OnItemCommand="RadGrid1_ItemCommand" OnNeedDataSource="RadGrid1_NeedDataSource"
       <MasterTableView  CommandItemDisplay="Top" EditMode="InPlace" Name="Master"
            <CommandItemTemplate> 
           <asp:Button ID="Button1" runat="server" CommandName="EditAll" Text="Edit All" /> 
           <asp:Button ID="Button2" runat="server" CommandName="SaveAll" Text="Save All" /> 
       </CommandItemTemplate> 
        
       <DetailTables >        
        <telerik:GridTableView EditMode="InPlace" DataSourceID="SqlDataSource1"  Name="Detail" runat="server" > 
        .... 

cs:
 
 protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) 
    { 
        
 
        if (e.CommandName == "SaveAll") 
        {            
               foreach (GridDataItem item in RadGrid1.Items) 
                { 
                    if (item.Expanded) 
                    {                        
                        GridTableView nestedView = (GridTableView)item.ChildItem.NestedTableViews[0]; 
                        foreach (GridEditableItem childItem in nestedView.GetItems(GridItemType.EditItem)) 
                        { 
                            string price = ((TextBox)childItem.FindControl("TextBox1")).Text; 
                            // Perform some kind of save to DB here 
                            childItem.Edit = false
                        } 
                    } 
                   item.Edit = false
                } 
                e.Item.OwnerTableView.Rebind();            
        }  
   } 

Thanks
Princy.
Tags
Grid
Asked by
Oleg Fridman
Top achievements
Rank 2
Answers by
Princy
Top achievements
Rank 2
Oleg Fridman
Top achievements
Rank 2
Share this question
or