windowsforms-403037-common-features-bind-to-mongo-db-data.md
MongoDB is a general purpose, document-based, distributed database built for modern application developers and for the cloud era. It stores data in JSON-like documents.
To bind a DevExpress data-aware control at design time, do the following.
Invoke the Data Source Configuration Wizard for this control.
Go to the “MongoDB Data Connection” tab and click “New Data Source…”. This button adds a new MongoDBDataSource component on the form.
Tip
Useful links:
BindingSource component or bind to data directly, and specify the data source to which you wish to connect.To bind a DevExpress data-aware control in code, you need to create and set up the MongoDBDataSource component.
Database and data collection names are stored in objects of the MongoDBQuery class, and a connection string is stored in a MongoDBCustomConnectionParameters object. See this link for information about connection string syntax: Connection String URI Format.
using DevExpress.DataAccess.ConnectionParameters;
using DevExpress.DataAccess.MongoDB;
public void InitMdbComponent() {
// connection parameters
MongoDBCustomConnectionParameters mongoDBCustomConnectionParameters1 =
new MongoDBCustomConnectionParameters();
mongoDBCustomConnectionParameters1.ConnectionString =
"mongodb+srv://[username:password@]host1[:port1][,...hostN[:portN]][/[defaultauthdb][?options]]";
mongoDBDataSource1.ConnectionParameters = mongoDBCustomConnectionParameters1;
// queries
MongoDBQuery mongoDBQuery1 = new MongoDBQuery();
mongoDBDataSource1.ConnectionName = "MdbConnection";
mongoDBQuery1.ActualName = "restaurants";
mongoDBQuery1.CollectionName = "restaurants";
mongoDBQuery1.DatabaseName = "sample_restaurants";
mongoDBDataSource1.Queries.Add(mongoDBQuery1);
// load data
mongoDBDataSource1.Fill();
}
Imports DevExpress.DataAccess.ConnectionParameters
Imports DevExpress.DataAccess.MongoDB
Public Sub InitMdbComponent()
' connection parameters
Dim mongoDBCustomConnectionParameters1 As New MongoDBCustomConnectionParameters()
mongoDBCustomConnectionParameters1.ConnectionString = "mongodb+srv://[username:password@]host1[:port1][,...hostN[:portN]][/[defaultauthdb][?options]]"
mongoDBDataSource1.ConnectionParameters = mongoDBCustomConnectionParameters1
' queries
Dim mongoDBQuery1 As New MongoDBQuery()
mongoDBDataSource1.ConnectionName = "MdbConnection"
mongoDBQuery1.ActualName = "restaurants"
mongoDBQuery1.CollectionName = "restaurants"
mongoDBQuery1.DatabaseName = "sample_restaurants"
mongoDBDataSource1.Queries.Add(mongoDBQuery1)
' load data
mongoDBDataSource1.Fill()
End Sub
Once the component is ready, use the data-aware control’s API to connect it to your control. The code below illustrates how to bind the Data Grid to MongoDB data.
void OnFormLoad(object sender, EventArgs e) {
gridControl1.DataMember = "restaurants";
gridControl1.DataSource = mongoDBDataSource1;
}
Private Sub OnFormLoad(ByVal sender As Object, ByVal e As EventArgs)
gridControl1.DataMember = "restaurants"
gridControl1.DataSource = mongoDBDataSource1
End Sub