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

Request for clarification regarding dataset as datasource for nested tables

1 Answer 67 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Will
Top achievements
Rank 1
Will asked on 09 Dec 2013, 06:05 PM
Hello.

After you helped me in this thread, I was able to get nested tables working using a custom object.

I then revisited the topic, reread the links you provided and got it working with a dataset with datarelations established on the dataset.  However, the solution I arrived at was so convoluted, I'm fairly certain that it isn't the intended way to accomplish that goal.

Could someone please look at the code below, tell me if it's the way we're intended to do this, and if not, provide an example of the correct method?  I suspect I'm doing this incorrectly because of the all the casting going on down in the getChildItem f(x).

The workers class is for making the dataset, including the datarelations and some randomly generated data.  To save typing I'm just grabbing the custom object from when I was testing that approach, and turning it into a dataset.

The createReportFromDS and getChildItem are in the actual report object.  There are two table objects on the report: tblParent, and nested within it, tblChild.

Public Function createReportFromDS() As Byte()
    Dim ods As New ObjectDataSource
 
    ods.DataSource = cboSample.workers.getWorkers
    ods.DataMember = "workers" 'casing has to match
    tblParent.DataSource = ods
    tblChild.Bindings.Add(New Telerik.Reporting.Binding("DataSource", "=getChildItem(ReportItem.DataObject)"))
 
    Return generateReportObj()
End Function
 
Public Shared Function getChildItem(sender As Object) As DataTable
    Dim dataObject = DirectCast(sender, Telerik.Reporting.Processing.IDataObject)
    Dim dt As DataTable = CType(CType(dataObject.RawData, DataRowView).Row, DataRow).GetChildRows("WtoT").CopyToDataTable()
 
    Return dt
End Function




Public Class workers
    Public Shared Function getWorkers() As DataSet
        Dim ds As DataSet = getDS()
        Dim myCbo As New dataItems
        Dim myWorkers As IEnumerable(Of parentItem) = myCbo.GetData 'to save typing just grab the custom object previously created and turn it into tables in a dataset
        Dim i As Integer
 
 
        'process the items
        For Each w As parentItem In myWorkers
            ds.Tables("workers").Rows.Add(i, w.worker, w.age)
            For Each t As childItem In w.tasks
                ds.Tables("tasks").Rows.Add(i, t.taskName, t.timeRequired, t.difficulty.ToString)
            Next
            i += 1
        Next
 
        Return ds
    End Function
 
    ''' <summary>
    ''' Returns a ds with columns and datarelations established
    ''' </summary>
    ''' <returns></returns>
    Private Shared Function getDS() As DataSet
        Dim dtP, dtC As DataTable
        Dim ds As New DataSet
 
        dtP = New DataTable("workers")
        dtP.Columns.Add("pk", GetType(Integer))
        dtP.Columns.Add("worker")
        dtP.Columns.Add("age")
        dtP.PrimaryKey = New DataColumn() {dtP.Columns("pk")}
 
        dtC = New DataTable("tasks")
        dtC.Columns.Add("fk", GetType(Integer))
        dtC.Columns.Add("taskName")
        dtC.Columns.Add("timeRequired")
        dtC.Columns.Add("difficulty")
 
        ds.Tables.AddRange(New DataTable() {dtP, dtC})
 
        ds.Relations.Add("WtoT", ds.Tables("workers").Columns("pk"), ds.Tables("tasks").Columns("fk"))
 
        Return ds
    End Function
End Class

1 Answer, 1 is accepted

Sort by
0
Nasko
Telerik team
answered on 12 Dec 2013, 03:47 PM
Hello Will,

The intended way to use nested collections in master-detail scenarios is elaborated in the How to use ReportItem.DataObject property in expressions, requires you to have a nested collection in your custom object in the first place. This means that you could have used the binding "= ReportItem.DataObject" for the nested table if the Worker object had a nested collection of Task objects in your business model, and omit most of the code that you are now using.

However, if it is not possible to reorganize the business model you can still use your approach, although at the cost of a more complex code.

Regards,
Nasko
Telerik

New HTML5/JS REPORT VIEWER with MOBILE AND TOUCH SUPPORT available in Telerik Reporting Q3 2013! Get the new Reporting version from your account or download a trial.

Tags
General Discussions
Asked by
Will
Top achievements
Rank 1
Answers by
Nasko
Telerik team
Share this question
or