New to Telerik Reporting? Download free 30-day trial

Binding ObjectDataSource Component to a DataView

The following example illustrates how to use a DataView as the source for an ObjectDataSource component. The example demonstrates also how to configure the report to use the ObjectDataSource and how to preview it in the Windows Forms Report Viewer with code.

The ObjectDataSource component may be configured in the Telerik Report Designers without the need for writing additional code. It may be necessary to extend the Reporting engine to recognize the custom assembly with the data providing logic through the assemblyReferences Element in the Telerik.Reporting section of the designer's configuration file.

The sample code below shows how to pass a DataView object to the DataSource property of the ObjectDataSource component. DataView is usually used when we want to work only with a subset of data from the DataTable. Additionally a sample calculated field is added that can be used in the report definition in the same way as a regular field.

void Form3_Load(object sender, EventArgs e)
{
    // Creating and configuring the ObjectDataSource component:
    var objectDataSource = new Telerik.Reporting.ObjectDataSource();
    objectDataSource.DataSource = GetData("Name LIKE 'Mountain%'"); // GetData returns a DataView with the specified row filter.
    objectDataSource.CalculatedFields.Add(new Telerik.Reporting.CalculatedField("FullName", typeof(string), "=Fields.Name + ' ' + Fields.ProductNumber")); // Adding a sample calculated field.

    // Creating a new report
    var report = new Report1();

    // Assigning the ObjectDataSource component to the DataSource property of the report.
    report.DataSource = objectDataSource;

    // Use the InstanceReportSource to pass the report to the viewer for displaying
    var reportSource = new Telerik.Reporting.InstanceReportSource();
    reportSource.ReportDocument = report;

    // Assigning the report to the report viewer.
    reportViewer1.ReportSource = reportSource;

    // Calling the RefreshReport method in case this is a WinForms application.
    reportViewer1.RefreshReport();
}

static DataView GetData(string rowFilter)
{
    const string connectionString =
        "Data Source=(local)\\SQLEXPRESS;Initial Catalog=AdventureWorks;Integrated Security=True";

    string selectCommandText =
        "SELECT Name, ProductNumber FROM Production.Product;";

    SqlDataAdapter dataAdapter = new SqlDataAdapter(selectCommandText, connectionString);
    DataTable dataTable = new DataTable();

    dataAdapter.Fill(dataTable);

    DataView dataView = dataTable.DefaultView;
    dataView.RowFilter = rowFilter; // View only those records that match a certain criteria.

    return dataView;
}
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)

    ' Creating and configuring the ObjectDataSource component:
    Dim objectDataSource As New Telerik.Reporting.ObjectDataSource()
    objectDataSource.DataSource = GetData("Name LIKE 'Mountain%'") ' GetData returns a DataView with the specified row filter.
    objectDataSource.CalculatedFields.Add(New Telerik.Reporting.CalculatedField("FullName", GetType(String), "=Fields.Name + ' ' + Fields.ProductNumber")) ' Adding a sample calculated field.


    'Creating a new report
    Dim report As New Report1()

    ' Assigning the ObjectDataSource component to the DataSource property of the report.
    report.DataSource = objectDataSource

    ' Use the InstanceReportSource to pass the report to the viewer for displaying.
    Dim reportSource As New InstanceReportSource
    reportSource.ReportDocument = report

    ' Assigning the report to the report viewer.
    reportViewer1.ReportSource = reportSource

    ' Calling the RefreshReport method in case this is a WinForms application.
    reportViewer1.RefreshReport()

End Sub

Shared Function GetData(ByVal rowFilter As String) As DataView
    Const connectionString As String =
        "Data Source=(local)\SQLEXPRESS;Initial Catalog=AdventureWorks;Integrated Security=True"

    Dim selectCommandText As String =
        "SELECT Name, ProductNumber FROM Production.Product;"

    Dim dataAdapter = New SqlDataAdapter(selectCommandText, connectionString)
    Dim dataTable As New DataTable()
    dataAdapter.Fill(dataTable)

    Dim dataView = dataTable.DefaultView
    dataView.RowFilter = rowFilter

    Return dataView
End Function

To use this code in the Report Designer Tools, the data retrival methods must be built in separate assembly and specifically configured before it can be resolved by the Report Designers - Connecting the ObjectDataSource component to a Data Source

See Also

In this article