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

3 Level Grid Hierarchical sample : help

1 Answer 76 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Pierre
Top achievements
Rank 1
Pierre asked on 27 Aug 2008, 09:10 PM
Hi,

I try with the 3 level sample for hierarchical Insert/delete. I have lost the insert for level 1 and level2, only master allow Insert operations. I have activated Autoinsert methods with no result. I've searched but no solution. Why I'm not able to insert in my tables?
Master :     loc_Ontrip                              poin_id(int auto)  <--Insert OK
level1:        loc_Ontrip_service                 service_id(int auto), point_id(int)
level2:        loc_Ontrip_service_room        room_id(int auto), service_id(int)
I have tested with point_id, service_id at level 1 and 2 as string, with no result.
I have tested with complete queries Insert, Update, Delete methods or simplefied like this post with only select methods for level1, and 2
I use the telerik sample with no modifications !
aspx
 <telerik:RadGrid ID="RadGrid1" DataSourceID="SessionDataSource1" runat="server" Skin="Telerik" ShowStatusBar="true" 
            Width="95%" AutoGenerateColumns="False" PageSize="3" AllowSorting="True" AllowMultiRowSelection="False" 
            AllowPaging="True" GridLines="None" AllowAutomaticDeletes="True" AllowAutomaticInserts="True" AllowAutomaticUpdates="True" 
            OnItemUpdated="RadGrid1_ItemUpdated" OnItemDeleted="RadGrid1_ItemDeleted" OnItemInserted="RadGrid1_ItemInserted" OnInsertCommand="RadGrid1_InsertCommand">  
            <PagerStyle Mode="NumericPages"></PagerStyle> 
              
            <MasterTableView DataSourceID="SessionDataSource1" DataKeyNames="point_id" AllowMultiColumnSorting="True" 
                Width="100%" CommandItemDisplay="Top" Name="Hotel">  
                <DetailTables> 
                    <%--level2 Services--%> 
                    <telerik:GridTableView DataKeyNames="service_id" DataSourceID="SessionDataSource2" Width="100%" 
                        runat="server" CommandItemDisplay="Top" Name="Services" AllowAutomaticInserts="true">  
                        <ParentTableRelation> 
                            <telerik:GridRelationFields DetailKeyField="point_id" MasterKeyField="point_id" /> 
                        </ParentTableRelation> 
                        <DetailTables> 
                            <%--level3 Rooms --%> 
                            <telerik:GridTableView DataKeyNames="room_id" DataSourceID="SessionDataSource3" Width="100%" 
                                runat="server" CommandItemDisplay="Top" Name="Rooms" AllowAutomaticInserts="true" > 
                                <ParentTableRelation> 
                                    <telerik:GridRelationFields DetailKeyField="service_id" MasterKeyField="service_id" /> 
                                </ParentTableRelation> 
                                <Columns> 
                                    <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn1">  
                                        <HeaderStyle Width="20px" /> 
                                        <ItemStyle CssClass="MyImageButton" /> 
                                    </telerik:GridEditCommandColumn> 
                                    <telerik:GridBoundColumn SortExpression="room_id" HeaderText="Rooms" HeaderButtonType="TextButton" 
                                        DataField="room_id" UniqueName="room_id" ReadOnly="true" Visible="false">  
                                    </telerik:GridBoundColumn> 
                                    <telerik:GridBoundColumn SortExpression="Price" HeaderText="UPrice" HeaderButtonType="TextButton" 
                                        DataField="Price" UniqueName="Price">  
                                    </telerik:GridBoundColumn> 
                                    <telerik:GridBoundColumn SortExpression="PriceWE" HeaderText="PriceWE" HeaderButtonType="TextButton" 
                                        DataField="PriceWE" UniqueName="PriceWE">  
                                    </telerik:GridBoundColumn> 
                                    <telerik:GridBoundColumn SortExpression="Discount" HeaderText="Discount" HeaderButtonType="TextButton" 
                                        DataField="Discount" UniqueName="Discount">  
                                    </telerik:GridBoundColumn> 
                                    <telerik:GridButtonColumn ConfirmText="Delete this room?" ButtonType="ImageButton" CommandName="Delete" Text="Delete" UniqueName="DeleteColumn1">  
                                        <HeaderStyle Width="20px" /> 
                                        <ItemStyle HorizontalAlign="Center" CssClass="MyImageButton" /> 
                                    </telerik:GridButtonColumn> 
                                </Columns> 
                                <SortExpressions> 
                                    <telerik:GridSortExpression FieldName="PriceWE" SortOrder="Descending"></telerik:GridSortExpression> 
                                </SortExpressions> 
                             </telerik:GridTableView> 
                        </DetailTables> 
                        <Columns> 
                            <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn2">  
                                <HeaderStyle Width="20px" /> 
                                <ItemStyle CssClass="MyImageButton" /> 
                            </telerik:GridEditCommandColumn> 
                            <telerik:GridBoundColumn SortExpression="service_id" HeaderText="service_id" HeaderButtonType="TextButton" 
                                DataField="service_id" UniqueName="service_id" ReadOnly="true">  
                            </telerik:GridBoundColumn> 
                            <telerik:GridBoundColumn SortExpression="service_name" HeaderText="Service" HeaderButtonType="TextButton" 
                                DataField="service_name" UniqueName="service_name" ReadOnly="true">  
                            </telerik:GridBoundColumn> 
                            <telerik:GridButtonColumn ConfirmText="Delete this service?" ButtonType="ImageButton" CommandName="Delete" Text="Delete" UniqueName="DeleteColumn2">  
                                <HeaderStyle Width="20px" /> 
                                <ItemStyle HorizontalAlign="Center" CssClass="MyImageButton" /> 
                            </telerik:GridButtonColumn> 
                        </Columns> 
                        <SortExpressions> 
                            <telerik:GridSortExpression FieldName="service_name"></telerik:GridSortExpression> 
                        </SortExpressions> 
                     </telerik:GridTableView> 
                </DetailTables> 
                <Columns> 
                    <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn">  
                        <HeaderStyle Width="20px" /> 
                        <ItemStyle CssClass="MyImageButton" /> 
                    </telerik:GridEditCommandColumn> 
                    <telerik:GridBoundColumn SortExpression="point_id" HeaderText="point_id" HeaderButtonType="TextButton" 
                        DataField="point_id" UniqueName="point_id" ReadOnly="true">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn SortExpression="point_name" HeaderText="Name" HeaderButtonType="TextButton" 
                        DataField="point_name" UniqueName="point_name">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn SortExpression="point_adress" HeaderText="Adress" HeaderButtonType="TextButton" 
                        DataField="point_adress" UniqueName="point_adress">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridButtonColumn ConfirmText="Delete this hotel?" ButtonType="ImageButton" CommandName="Delete" Text="Delete" UniqueName="DeleteColumn">  
                        <HeaderStyle Width="20px" /> 
                        <ItemStyle HorizontalAlign="Center" CssClass="MyImageButton" /> 
                    </telerik:GridButtonColumn> 
                </Columns> 
                <SortExpressions> 
                    <telerik:GridSortExpression FieldName="point_adress"></telerik:GridSortExpression> 
                </SortExpressions> 
            </MasterTableView> 
        </telerik:RadGrid> 
          
        <%--DATASOURCES --%> 
        <asp:SqlDataSource ID="SessionDataSource1" ConnectionString="<%$ ConnectionStrings:MyProv %>" 
            ProviderName="<%$ ConnectionStrings:MyProv.ProviderName %>" 
            SelectCommand="SELECT * FROM loc_OnTrip" runat="server"   
             OldValuesParameterFormatString="original_{0}" ConflictDetection="CompareAllValues">  
              
        </asp:SqlDataSource> 
          
        <asp:SqlDataSource ID="SessionDataSource2" ConnectionString="<%$ ConnectionStrings:MyProv %>"   
            ProviderName="<%$ ConnectionStrings:MyProv.ProviderName %>" 
            SelectCommand="SELECT * FROM [loc_OnTrip_Service] Where point_id = @point_id" runat="server"   
            OldValuesParameterFormatString="original_{0}" ConflictDetection="CompareAllValues" > 
            <SelectParameters> 
                <asp:Parameter Name="point_id" Type="string" />   
            </SelectParameters> 
        </asp:SqlDataSource> 
          
        <asp:SqlDataSource ID="SessionDataSource3" ConnectionString="<%$ ConnectionStrings:MyProv%>"   
            ProviderName="<%$ ConnectionStrings:MyProv.ProviderName %>"   
            SelectCommand="SELECT * FROM [loc_OnTrip_Service_room] where service_id = @service_id" runat="server"   
            OldValuesParameterFormatString="original_{0}" ConflictDetection="CompareAllValues" > 
            <SelectParameters> 
                <asp:Parameter Name="service_id" Type="string" />   
            </SelectParameters> 
        </asp:SqlDataSource> 
        <br /> 

aspx.cs
private void Page_Load(object sender, EventArgs e)  
        {  
            if (!IsPostBack)  
            {  
                //Setting Selected index prior to binding RadGrid:  
                //If the index is in detail table, parent items will be expanded  
                //automatically   
                RadGrid1.SelectedIndexes.Add(1, 0, 1, 0, 0);  
                //Index of 1, 0, 1, 0, 0 means:  
                //1 - item with index 1 in the MasterTabelView  
                //0 - detail table with index 0  
                //1 - item with index 1 (the second item) in the first detail table  
                //0 - 0 the third-level detail table  
                //0 - the item with index 0 in the third-level table  
            }  
        }  
 
        protected void RadGrid1_ItemUpdated(object source, Telerik.Web.UI.GridUpdatedEventArgs e)  
        {  
            string item = getItemName(e.Item.OwnerTableView.Name);  
            string field = getFieldName(e.Item.OwnerTableView.Name);  
            if (e.Exception != null)  
            {  
                e.KeepInEditMode = true;  
                e.ExceptionHandled = true;  
                DisplayMessage(item + " " + e.Item[field].Text + " cannot be updated. Reason: " + e.Exception.Message);  
            }  
            else  
            {  
                DisplayMessage(item + " " + e.Item[field].Text + " updated");  
            }  
        }  
 
        protected void RadGrid1_ItemInserted(object source, GridInsertedEventArgs e)  
        {  
            string item = getItemName(e.Item.OwnerTableView.Name);  
            if (e.Exception != null)  
            {  
                e.ExceptionHandled = true;  
                DisplayMessage(item + " cannot be inserted. Reason: " + e.Exception.Message);  
            }  
            else  
            {  
                DisplayMessage(item + " inserted");  
            }  
        }  
 
        protected void RadGrid1_ItemDeleted(object source, GridDeletedEventArgs e)  
        {  
            string item = getItemName(e.Item.OwnerTableView.Name);  
            string field = getFieldName(e.Item.OwnerTableView.Name);  
            if (e.Exception != null)  
            {  
                e.ExceptionHandled = true;  
                DisplayMessage(item + " " + e.Item[field].Text + " cannot be deleted. Reason: " + e.Exception.Message);  
            }  
            else  
            {  
                DisplayMessage(item + " " + e.Item[field].Text + " deleted");  
            }  
        }  
 
        protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)  
        {  
            if ("Service".Equals(e.Item.OwnerTableView.Name))  
            {  
                GridDataItem parentItem = (GridDataItem)e.Item.OwnerTableView.ParentItem;  
                SessionDataSource2.InsertParameters["point_id"].DefaultValue = parentItem.OwnerTableView.DataKeyValues[parentItem.ItemIndex]["point_id"].ToString();  
            }  
            else if ("Rooms".Equals(e.Item.OwnerTableView.Name))  
            {  
                GridDataItem parentItem = (GridDataItem)e.Item.OwnerTableView.ParentItem;  
                SessionDataSource3.InsertParameters["service_id"].DefaultValue = parentItem.OwnerTableView.DataKeyValues[parentItem.ItemIndex]["service_id"].ToString();  
            }  
        }  
        /* Sample
         protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)  
    {  
        GridDataItem parentItem = e.Item.OwnerTableView.ParentItem as GridDataItem;  
        nestedTableSource.InsertParameters["EmployeeID"].DefaultValue = parentItem.OwnerTableView.DataKeyValues[parentItem.ItemIndex]["EmployeeID"].ToString();  
    }  
           
         */  
        private String getItemName(string tableName)  
        {  
            switch (tableName)  
            {  
                case ("Hotel"):  
                    {  
                        return "loc_Ontrip";// for Hotels  
                    }  
                case ("Service"):  
                    {  
                        return "loc_OnTrip_Service";// for services  
                    }  
                case ("Rooms"):  
                    {  
                        return "loc_OnTrip_Services_room"; // for Rooms  
                    }  
                default: return "";  
            }  
        }  
 
        private String getFieldName(string tableName)  
        {  
            switch (tableName)  
            {  
                case ("loc_Ontrip"):  
                    {  
                        return "point_id";  
                    }  
                case ("loc_OnTrip_Service"):  
                    {  
                        return "service_id";  
                    }  
                case ("loc_OnTrip_Services_room"):  
                    {  
                        return "room_id";  
                    }  
                default: return "";  
            }  
        }  
 
        private void DisplayMessage(string text)  
        {  
            RadGrid1.Controls.Add(new LiteralControl(string.Format("<span style='color:red'>{0}</span>", text)));  
        } 

1 Answer, 1 is accepted

Sort by
0
Pavel
Telerik team
answered on 01 Sep 2008, 02:34 PM
Hi Pierre,

Your SqlDataSource controls are missing the insert parameters necessary to handle the operation. You can automatically generate them in design-time. Give this suggestion a try and let us know how it goes.

Best wishes,
Pavel
the Telerik team

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