Example T997274
Visible to All Users

Reporting for WinForms - Bind a Report to a MongoDB Instance

Create and set up a MongoDBDataSource object to bind a report to a MongoDB instance in code.

  • Specify connection parameters to connect to a MongoDB instance. You can create a MongoDBConnectionParameters
    class instance and specify connection parameters individually, such as a hostname and port. If you want to use a connection string, you can create a MongoDBCustomConnectionParameters object
    and assign this string to the object's ConnectionString property.
    Assign the created connection parameters to the MongoDB data source's ConnectionParameter property.
  • Create queries to retrieve data from database collections of a MongoDB instance. An object of a MongoDBQuery class corresponds to one query. Set
    the query's DatabaseName and CollectionName
    properties to the name of a database and collection from which you want to load data. You can also filter the collection's items. For this, assign a filter condition to the query's FilterString property.
    Add the created queries to the MongoDB data source's Queries collection.

[!NOTE]
This example uses the following connection string to connect to a MongoDB instance: mongodb://localhost:27017. The example also specifies queries to load data from the collections of the Northwind database. Before you run this example, specify a connection string and data queries to a configured MongoDB instance.

Files to Review

Documentation

Example Code

Form1.cs(vb)
C#
#region usings using DevExpress.DataAccess.ConnectionParameters; using DevExpress.DataAccess.MongoDB; using DevExpress.XtraReports.UI; #endregion using System; using System.Windows.Forms; namespace Bind_a_Report_to_a_MongoDB_Instance { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { #region bindReportToMongoDBInstanceExample // Create a MongoDBCustomConnectionParameters object and assign // a connection string to a MongoDB instance to the object's // ConnectionString property. var connectionString = new MongoDBCustomConnectionParameters() { ConnectionString = "mongodb://localhost:27017" }; // Specify data queries to the MongoDB instance. var queryCategories = new MongoDBQuery() { DatabaseName = "Northwind", CollectionName = "Categories", }; var queryProducts = new MongoDBQuery() { DatabaseName = "Northwind", CollectionName = "Products", }; // Create a MongoDBDataSource object. Assign the created connection // string to the object's ConnectionParameters property. Add the // queries to the object's Queries collection. var mongoDBDataSource = new MongoDBDataSource() { ConnectionParameters = connectionString, Queries = { queryCategories, queryProducts } }; // Create a report. Set the report's DataSource property // to the created mongoDBDataSource object. var report = new XtraReport() { DataSource = mongoDBDataSource, DataMember = "Categories" }; // Configure the report layout. // ... #endregion //mongoDBDataSource.RebuildResultSchema(); report.ShowDesigner(); } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.