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

Edit form across hierarchy groups

5 Answers 116 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Philip
Top achievements
Rank 1
Philip asked on 03 Oct 2008, 02:14 PM

I'm having a problem figuring out the correct way to populate an edit form with data when using a master/detail hierarchy.

The problem I'm having is when I show the edit form for a detail item in one hierarchy group the data in any visible edit form in the other hierarchy groups no longer has data so the edit form is empty. I'm using the edit form to display additional, static, information about the detail item.

The way I currently have it setup is to use the ItemDataBound event to check if the item is in edit mode and if it's a GridEditFormItem. From there I can get the DataItem from the item to populate labels in the edit form template. This works fine when showing the edit form for any of the detail rows in the current hierarchy group. However, checking e.Item.IsInEditMode only catches the items in the hierarchy group the expanded item is in. This means the other previously expanded hierarchy group detail rows never get repopulated with data so their form's are blank and introduces my problem.

I tried to update my code to use the ItemCreated event but for the items not in the current hierarchy group both the e.Item.DataItem and the edit form's ParentItem.DataItem are null.

Anyone have any suggestions or other questions that might help me track down the correct way to do this?

5 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 07 Oct 2008, 07:44 AM
Hello Philip,

Could you please modify the attached sample website to perform like yours? Thus we will be able to give you more to-the-point answer.

Regards,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Philip
Top achievements
Rank 1
answered on 08 Oct 2008, 05:07 PM
<Steps>
1. Run the project with the code changes below
2. Click the first > button to expand the "Beverages" category
3. Click the Edit link for "Chai"
4. Notice the edit form has the product name "Chai" showing.
5. With "Chai" expanded still click the second > to expand the "Condiments" category.
6. Notice the product details in the "Chai" edit form are now missing.

Code Behind
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)  
    {  
        GridEditFormItem edititem = e.Item as GridEditFormItem;  
        if (edititem != null && edititem.IsInEditMode)  
        {  
            DataRowView _data = e.Item.DataItem as DataRowView;  
            SetProductDetails(_data, edititem);  
        }  
    }  
 
    private void SetProductDetails(DataRowView prodData, Control editFormItem)  
    {  
        Panel _panel = editFormItem.FindControl("pnlProdDetails"as Panel;  
        if (_panel == null)  
        {  
            return;  
        }  
 
        PlaceHolder _placeHolder = _panel.FindControl("phProdDetails"as PlaceHolder;  
        if (_placeHolder == null)  
        {  
            return;  
        }  
 
        Label lblProdName = new Label();  
        lblProdName.Text = string.Format("Product Name: {0}<br/>", prodData["ProductName"]);  
        _placeHolder.Controls.Add(lblProdName);  
    } 

Markup
<telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1" GridLines="None" 
            AutoGenerateEditColumn="true" AllowMultiRowEdit="true" OnItemDataBound="RadGrid1_ItemDataBound">  
            <MasterTableView AutoGenerateColumns="True" DataSourceID="SqlDataSource1" DataKeyNames="CategoryID" 
                Name="MasterTableView" CommandItemDisplay="Top">  
                <DetailTables> 
                    <telerik:GridTableView DataKeyNames="CategoryID" AutoGenerateColumns="true" DataSourceID="SqlDataSource2" 
                        CommandItemDisplay="Top" AllowPaging="true" PageSize="3">  
                        <ParentTableRelation> 
                            <telerik:GridRelationFields DetailKeyField="CategoryID" MasterKeyField="CategoryID" /> 
                        </ParentTableRelation> 
                        <EditFormSettings EditFormType="Template">  
                            <FormTemplate> 
                                <asp:Panel runat="server" id="pnlProdDetails">  
                                    <asp:Label Font-Italic="true" runat="server" ID="lblProdDetails" Text="Product Details" /> 
                                    <br /> 
                                    <asp:PlaceHolder runat="server" ID="phProdDetails" /> 
                                    <br /> 
                                </asp:Panel> 
                            </FormTemplate> 
                        </EditFormSettings> 
                    </telerik:GridTableView> 
                </DetailTables> 
            </MasterTableView> 
        </telerik:RadGrid> 
0
Daniel
Telerik team
answered on 13 Oct 2008, 01:56 PM
Hello Philip,

Thank you for the detailed clarification. Please try the following sample approach:
    protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
    { 
        GridEditFormItem edititem = e.Item as GridEditFormItem; 
        if (edititem != null && edititem.IsInEditMode) 
        { 
            DataRowView _data = e.Item.DataItem as DataRowView; 
            SetProductDetails(_data, edititem); 
        } 
    } 
 
    private void SetProductDetails(DataRowView prodData, Control editFormItem) 
    { 
        Panel _panel = editFormItem.FindControl("pnlProdDetails"as Panel; 
        if (_panel == null
        { 
            return
        } 
 
        PlaceHolder _placeHolder = _panel.FindControl("phProdDetails"as PlaceHolder; 
        if (_placeHolder == null
        { 
            return
        } 
 
        Label lblProdName = _placeHolder.FindControl("lblProdName"as Label; 
        lblProdName.Text = string.Format("Product Name: {0}<br/>", prodData["ProductName"]); 
    } 
 
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
    { 
        GridEditFormItem edititem = e.Item as GridEditFormItem; 
        if (edititem != null && edititem.IsInEditMode) 
        { 
            Panel _panel = edititem.FindControl("pnlProdDetails"as Panel; 
            PlaceHolder _placeHolder = _panel.FindControl("phProdDetails"as PlaceHolder; 
            Label lblProdName = new Label(); 
            lblProdName.ID = "lblProdName"
            _placeHolder.Controls.Add(lblProdName); 
        } 
    } 

Best regards,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Philip
Top achievements
Rank 1
answered on 13 Oct 2008, 02:57 PM

Daniel,

Thank you for your reply. The changes you suggested do work for the example, and make sense, but unfortunately they will not work for me in my production code because I don't know the type, or number, of controls I'll need to create until I look at the detail data.

The way I currently have this working is by using the ItemCreated event and checking to see if the editFormItem.ParentItem.DataItem has an entity. If it is null I'll figure out the ID I need by parsing the editFormItem.KeyValues and doing a new query for the detail data with this ID. This grid is not on a heavily used page, and there won't be many situations where multiple detail rows have their details exposed, but I'm just not sure if the requery for the data is the best way to get to the data I need to add the dynamic controls.

0
Accepted
Daniel
Telerik team
answered on 16 Oct 2008, 12:09 PM
Hello Philip,

We'll be glad to assist you further if you need additional assistance. In this case you can submit a support ticket with simple working example which will allow us to examine the concrete scenario.

Best regards,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
Philip
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Philip
Top achievements
Rank 1
Share this question
or