Progress® Telerik® Reporting R2 2017

How to: Bind to a DataAdapter

The following example illustrates how to use a DataAdapter as the source for an ObjectDataSource component.

The ObjectDataSource component allows you to use directly a class that implements the System.Data.IDataAdapter interface. In the sample code bellow an instance of the SqlDataAdapter class is created and passed to the DataSource property of the ObjectDataSource component. When the DataAdapter encounters multiple result sets, it creates multiple tables and each table is given an incremental default name of TableN, starting with "Table" for Table0. In this case you have to set the DataMember property of the ObjectDataSource component to indicate exactly which table to bind to. Additionally a sample calculated field is added that can be used in the report definition in the same way as a regular field.

void Form4_Load(object sender, EventArgs e)
    // Creating and configuring the ObjectDataSource component:
    var objectDataSource = new Telerik.Reporting.ObjectDataSource();
    objectDataSource.DataSource = GetMyData(); // GetData returns a SqlDataAdapter object which has a select command with three SELECT statements.
    objectDataSource.DataMember = "Table2"; /// Indicating the exact table to bind to. If the DataMember is not specified the first data table will be used.
    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.

static IDataAdapter GetMyData()
    const string connectionString =
        "Data Source=(local)\\SQLEXPRESS;Initial Catalog=AdventureWorks;Integrated Security=True";

    string selectCommandText = "SELECT Name, ProductCategoryID FROM Production.ProductCategory;" +
        "SELECT Name, ProductCategoryID FROM Production.ProductSubcategory;" +
        "SELECT Name, ProductNumber FROM Production.Product;";

    return new SqlDataAdapter(selectCommandText, connectionString);