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

Insert and SelectedValue(s)

4 Answers 115 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ceramist
Top achievements
Rank 1
Ceramist asked on 29 Sep 2008, 04:33 AM
Currently, one can link a detail (child) grid to a master (parent) grid using LINQ.  First, two LingDataSources are established for each grid and then linked together using the WhereParameters of the detail (child) LinqDataSource to identifying the key fields which are then linked to the master grid using the SelectedValue field.  One example from Northwind db is given below:

<asp:LinqDataSource ID="LinqDataSource1" runat="server" 
    ContextTypeName="NWExample.DataClasses1DataContext" 
    EnableDelete="True" EnableInsert="True" EnableUpdate="True" TableName="Customers">  
</asp:LinqDataSource> 
 
<asp:LinqDataSource ID="LinqDataSource2" runat="server" 
    ContextTypeName="NWExample.DataClasses1DataContext" 
    EnableDelete="True" EnableInsert="True" EnableUpdate="True" TableName="Orders" 
    Where="CustomerID == @CustomerID">  
    <WhereParameters> 
        <asp:ControlParameter ControlID="RadGrid1" Name="CustomerID" 
        PropertyName="SelectedValues['CustomerID']" 
        Type="String" /> 
    </WhereParameters> 
</asp:LinqDataSource> 

To insert items into the detail grid, one needs to write some VB or C# code to place into the DataSource's InsertParameters a default value equal the SelectedValue of the master grid.  A recent update permits using RadGrid.SelectedValue['FieldName'] permitting use of a specific field linking the two grids.  Such as:

    Protected Sub RadGrid1_InsertCommand(ByVal source As ObjectByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.InsertCommand  
        If "Detail".Equals(e.Item.OwnerTableView.Name) Then 
            Dim parentItem As Telerik.Web.UI.GridDataItem = DirectCast(e.Item.OwnerTableView.ParentItem, Telerik.Web.UI.GridDataItem)  
            LinqDataSource2.InsertParameters("CustomerID").DefaultValue = parentItem.OwnerTableView.DataKeyValues(parentItem.ItemIndex)("CustomerID").ToString  
        End If 
    End Sub 

In the definition of InsertParameters for LINQ, I should be able to avoid using VB code using a "codeless" method.  For example, the LinqDataSource2 above should include the following

<InsertParameters> 
    <asp:ControlParameter ControlID="RadGrid1" Name="CustomerID" 
    PropertyName="SelectedValues['CustomerID']" Type="String" /> 
</InsertParameters> 

But I am not having success using this method.  When I try to insert a new record into the details grid, the value of CustomerID is not transfered to database but a "Null" value is supplied so I must use the VB code to set the default value of each InsertParameter.  Shouldn't the Insert, Update, Select, and Delete functions be able to access the RadGrid's SelectedValue fields?  Is this an issue with LINQ or with RadGrid?

I really like using "codeless" and allowing the intelligence of the components to interact together.  Please let me know where I my code is flawed.

4 Answers, 1 is accepted

Sort by
0
Nikolay Rusev
Telerik team
answered on 01 Oct 2008, 02:01 PM
Hello Walter,

Attached to this forum post you can find simple web site which has to pages. First one demonstrates two level hierarchy RadGrid with insert/update/delete enabled on both levels, and the second one two RadGrids in Master-Detail scenario again with insert/update/delete enabled as well.

On both examples grid controls are bound via LinqDataSource control completely codeless.

Best wishes,
Nikolay
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Ceramist
Top achievements
Rank 1
answered on 01 Oct 2008, 05:53 PM
Nikolay,

Thank you very much for the reply and the additonal code.  I agree, the two examples permit me to add records to the database without using any additional VB/C# code.  In addition to this functionality, I was hoping the software would automatically supply the key identified in the parent-table relationship instead of asking the user to provide that data.  For example, if I add a new order for a customer using your solution, the software already knows the CustomerID from the relationship and should provide that data to the inserted record.  This permits the user to rely upon the visual structure when working with their data and the developer to not worry about the basic data-handling using VB/C# code.  Any suggestions for a solution from the experts?  I really enjoy the Telerik products and we're soon to launch our internal application because of the product quality.

Thanks,

Walter
0
Nikolay Rusev
Telerik team
answered on 03 Oct 2008, 04:18 PM
Hello Walter,

Fortunately this functionality will be implemented for Q3 release of RadControls for ASP.NET AJAX.
The key from parent table view will be automatically taken and there will be no need to set them explicitly.

Regards,
Nikolay
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Ceramist
Top achievements
Rank 1
answered on 03 Oct 2008, 05:02 PM
Dude, that rocks!  Another great reason for buying this product line.

Thanks,

Walter
Tags
Grid
Asked by
Ceramist
Top achievements
Rank 1
Answers by
Nikolay Rusev
Telerik team
Ceramist
Top achievements
Rank 1
Share this question
or