Skip to main content
All docs
V25.1
  • Bind the Grid to a MongoDB Database

    • 2 minutes to read

    This tutorial binds the DevExpress WinForms GridControl to a collection of documents from a MongoDB database and displays a master-detail relationship. The tutorial uses a list of Order documents, each of which contains a list of Product items.

    Note

    This tutorial assumes that MongoDB is already installed and the Orders collection is populated.

    Prerequisites

    Install the MongoDB.Driver NuGet package.

    Create Data Classes

    Create the following classes that match the following MongoDB document schema:

    MongoDB Documents

    using MongoDB.Bson;
    using MongoDB.Bson.Serialization.Attributes;
    using System;
    using System.Collections.Generic;
    
    public class Order {
        [BsonId]
        [BsonRepresentation(BsonType.ObjectId)]
        public string Id { get; set; }
    
        [BsonElement("CreateDate")]
        public DateTime CreateDate { get; set; }
    
        [BsonElement("Price")]
        public double Price { get; set; }
    
        [BsonElement("Products")]
        public List<Product> Products { get; set; }
    }
    
    public class Product {
        [BsonElement("ProductId")]
        public string ProductId { get; set; }
    
        [BsonElement("Name")]
        public string Name { get; set; }
    
        [BsonElement("Quantity")]
        public int Quantity { get; set; }
    
        [BsonElement("UnitPrice")]
        public double UnitPrice { get; set; }
    }
    
    JSON Data - MongoDB Document Schema
    [
      {
        "CreateDate": "2024-05-01T10:15:00Z",
        "Price": 199.99,
        "Products": [
          {
            "ProductId": "P-001",
            "Name": "Wireless Mouse",
            "Quantity": 2,
            "UnitPrice": 29.99
          },
          {
            "ProductId": "P-002",
            "Name": "USB-C Cable",
            "Quantity": 1,
            "UnitPrice": 19.99
          }
        ]
      },
      {
        "CreateDate": "2024-05-03T14:30:00Z",
        "Price": 349.50,
        "Products": [
          {
            "ProductId": "P-003",
            "Name": "Mechanical Keyboard",
            "Quantity": 1,
            "UnitPrice": 120.00
          },
          {
            "ProductId": "P-004",
            "Name": "HD Monitor",
            "Quantity": 1,
            "UnitPrice": 229.50
          }
        ]
      }
    ]
    

    Connect to MongoDB and Fetch Data

    Use the MongoClient class to retrieve documents from the Orders collection:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using MongoDB.Driver;
    using MongoDB.Bson;
    using MongoDB.Bson.Serialization.Attributes;
    
    public partial class Form1 : DevExpress.XtraEditors.XtraForm {
        public Form1() {
            InitializeComponent();
    
            // Replace with your connection string.
            var client = new MongoClient("mongodb://localhost:27017");
    
            var database = client.GetDatabase("orders");
            var collection = database.GetCollection<Order>("Order");
    
            var data = collection.Find(FilterDefinition<Order>.Empty).ToList();
        }
    }
    

    Bind the GridControl

    Add a GridControl to your form and bind it to MongoDB data:

    using DevExpress.XtraGrid;
    using DevExpress.XtraGrid.Views.Grid;
    
    public partial class Form1 : DevExpress.XtraEditors.XtraForm {
        GridControl gridControl;
        GridView gridView;
        public Form1() {
            InitializeComponent();
            // ...
            var data = collection.Find(FilterDefinition<Order>.Empty).ToList();
    
            gridView = new GridView();
            // Enable master-detail mode.
            gridView.OptionsDetail.EnableMasterViewMode = true;
    
            gridControl = new GridControl() {
                Dock = DockStyle.Fill,
                MainView = gridView,
                DataSource = data
            };
    
            this.Controls.Add(gridControl);
        }
    }
    

    When the form loads, the GridControl displays a list of orders. Each row displays an expand button that reveals the associated product list in a detail view:

    Display Master-Detail Data from MongoDB - WinForms Data Grid, DevExpress

    See Also