Skip to main content
A newer version of this page is available. .

Bind a Report to a DataSet

  • 4 minutes to read

This topic explains how to bind a report to a DataSet object.

Important

If you use the End-User Report Designer, be aware that non-serializable objects (for instance, DataSet) are lost when you save a report.

We recommend that you use one of the built-in serializable data components instead. Use the Data Source Wizard to create them.

Design Time Binding

Do the following to bind a report to a DataSet object at design time in Visual Studio:

  1. Create a new application or open an existing application.

  2. Select Add New Data Source on the Project menu.

    bind-a-report-to-dataset-01

  3. From the invoked Data Source Configuration Wizard, select Database and click Next.

    bind-a-report-to-dataset-02

  4. Choose the Dataset as a database model. Click Next.

    bind-a-report-to-dataset-03

  5. On the next page, click New Connection.

    bind-a-report-to-dataset-04

  6. In this example, the report is bound to a sample Northwind database hosted on a Microsoft SQL Server. Specify the connection options. Click OK.

    bind-a-report-to-dataset-05

  7. Click Next to connect to the database.

  8. On the next page, choose which object to add to the dataset. Expand the Tables category, select the Categories item and click Finish to exit the wizard.

    bind-a-report-to-dataset-06

  9. Add a new blank report to the application.

  10. Click the report’s smart tag. In the invoked actions list, choose DataSource - Project Options - NWindDataSet.

    bind-a-report-to-dataset-07 bind-a-report-to-dataset-08

  11. The report is now bound to data. The Report Explorer displays the data source in the Components node. The Field List reflects the data source’s hierarchy.

    bind-a-report-to-dataset-09

Runtime Binding

The following example demonstrates how to bind a report to a DataSet object at runtime:

using DevExpress.XtraReports.UI;
//...
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        ReportDesignTool designTool = new ReportDesignTool(CreateReport());
        designTool.ShowRibbonDesignerDialog();
    }

    XtraReport CreateReport()
    {
        // Create a dataset.           
        DataSet ds = FillDataset();
        // Define a report
        XtraReport report = new XtraReport()
        {
            DataSource = ds,
            DataMember = ds.Tables[0].TableName,
            Bands = {
                new DetailBand()
                {
                    Controls = {
                        CreateLabel()
                        }
                }
            }
        };
        return report;
    }

    XRLabel CreateLabel()
    {
        ExpressionBinding eb = new ExpressionBinding("BeforePrint", "Text", "[ProductId] + ' | ' + [ProductName] + ' | ' + [SupplierId] + ' | ' + [Category]");
        XRLabel lb = new XRLabel { Left = 30, WidthF = 300 };
        lb.ExpressionBindings.Add(eb);
        return lb;
    }


    public DataSet FillDataset()
    {
        DataSet dataSet1 = new DataSet();
        dataSet1.DataSetName = "nwindDataSet1";
        DataTable dataTable1 = new DataTable();

        dataSet1.Tables.Add(dataTable1);

        dataTable1.TableName = "Products";
        dataTable1.Columns.Add("ProductId", typeof(int));
        dataTable1.Columns.Add("ProductName", typeof(string));
        dataTable1.Columns.Add("SupplierId", typeof(int));
        dataTable1.Columns.Add("Category", typeof(int));


        dataSet1.Tables["Products"].Rows.Add(new Object[] { 1, "Chai", 1, 1 });
        dataSet1.Tables["Products"].Rows.Add(new Object[] { 2, "Chang", 1, 1 });
        dataSet1.Tables["Products"].Rows.Add(new Object[] { 3, "Aniseed Syrup", 1, 2 });
        dataSet1.Tables["Products"].Rows.Add(new Object[] { 4, "Chef Anton's Cajun Seasoning", 2, 2 });
        dataSet1.Tables["Products"].Rows.Add(new Object[] { 5, "Chef Anton's Gumbo Mix", 2, 2 });
        return dataSet1;

    }
}

The following image demonstrates the resulting Field List:

Result