Skip to main content

Bind a Report to a Data Source Schema

  • 4 minutes to read

This tutorial demonstrates how to bind a report to a data source schema and supply data at runtime. Use this technique if the report’s data source is unavailable at design time.

Note

When a report is bound to an XML file, you cannot use the following data-shaping capabilities at the data source level:

  • Sort, group and filter data
  • Aggregate functions

Create a Data Source Schema

Use the WriteXmlSchema method to create an XSD file that contains a data source’s schema. Refer to the Create and configure datasets in Visual Studio documentation topic for more information.

The following code writes the XSD schema to a file:

nwindDataSet ds = new nwindDataSet();
ds.WriteXmlSchema(@"C:/Temp/1.xsd");

Use the XSD file as a data source schema for a new report. The following example contains the XML schema for the Products table from the sample Northwind database.

<?xml version="1.0" standalone="yes"?>
<xs:schema id="nwindDataSet" targetNamespace="http://tempuri.org/nwindDataSet.xsd" 
    xmlns:mstns="http://tempuri.org/nwindDataSet.xsd" 
    xmlns="http://tempuri.org/nwindDataSet.xsd" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified" elementFormDefault="qualified">
    <xs:element name="nwindDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
        <xs:complexType>
            <xs:choice minOccurs="0" maxOccurs="unbounded">
                <xs:element name="Products">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="ProductID" msdata:AutoIncrement="true" type="xs:int" />
                            <xs:element name="ProductName" minOccurs="0">
                                <xs:simpleType>
                                    <xs:restriction base="xs:string">
                                        <xs:maxLength value="40" />
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:element>
                            <xs:element name="SupplierID" type="xs:int" minOccurs="0" />
                            <xs:element name="CategoryID" type="xs:int" minOccurs="0" />
                            <xs:element name="QuantityPerUnit" minOccurs="0">
                                <xs:simpleType>
                                    <xs:restriction base="xs:string">
                                        <xs:maxLength value="20" />
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:element>
                            <xs:element name="UnitPrice" type="xs:decimal" minOccurs="0" />
                            <xs:element name="UnitsInStock" type="xs:short" minOccurs="0" />
                            <xs:element name="UnitsOnOrder" type="xs:short" minOccurs="0" />
                            <xs:element name="ReorderLevel" type="xs:short" minOccurs="0" />
                            <xs:element name="Discontinued" type="xs:boolean" minOccurs="0" />
                            <xs:element name="EAN13" minOccurs="0">
                                <xs:simpleType>
                                    <xs:restriction base="xs:string">
                                        <xs:maxLength value="12" />
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:choice>
        </xs:complexType>
        <xs:unique name="Constraint1" msdata:PrimaryKey="true">
            <xs:selector xpath=".//mstns:Products" />
            <xs:field xpath="mstns:ProductID" />
        </xs:unique>
    </xs:element>
</xs:schema>

Assign the Data Source Schema at Design Time

  1. Start Microsoft Visual Studio and create a new application, or open an existing application.
  2. Add a new blank report to it.
  3. Click the report’s smart tag. In the invoked actions list, expand the DataSource property’s drop-down menu. Click Add Report Data Source.

    how-to-ef-datasource01

  4. From the invoked Data Source Wizard, select XML file and click Next.

    ReportWizard-SelectDataSourceType

  5. On the next page, select the first option to create a new data connection. Click Next.

    data-source-wizard-specify-data-connection

  6. Specify the path to the database schema file. Click Next.

    sql-data-source-xsd

  7. Click Next to save the connection string to the configuration file.

  8. On the next page, you can choose which tables, views and/or stored procedures to add to the report. To construct custom queries, use the Query Builder.

    data-source-wizard-create-query-products

    Click Finish to exit the wizard.

    The Report Explorer displays the created SQL data source in the Data Sources node.

    how-to-sql-data-source02

    The Field List reflects the data source’s hierarchy. Drop the required fields onto the report’s Detail band from the Field List, to create report controls bound to these fields.

    sql-data-source-xsd-field-list

Obtain the Data and View the Result

At runtime, create (or get) an instance of the data source. This data source should have the same type that you specified for the schema at design time.

private void button1_Click(object sender, EventArgs e) {
    // Obtain a dataset or create a new one.
    // For example:
    nwindDataSet ds = new nwindDataSet();
    new nwindDataSetTableAdapters.ProductsTableAdapter().Fill(ds.Products);

    // Create a report and bind it to a dataset.
    XtraReport1 report = new XtraReport1();
    report.DataSource = ds;

    // Show the print preview.
    ReportPrintTool pt = new ReportPrintTool(report);
    pt.ShowPreview();
}

The report is bound to data. Run the print preview form to view the result.

HowTo - BindAtDesignCreateAtRunTime_2

See Also