Skip to main content
All docs
V23.2

Manage Table Relations

Use the SelectQueryFluentBuilder object to create two queries and add a relationship between resulting tables. This data is used to configure a master-detail report.

To add a relationship between two tables, add a MasterDetailInfo instance to the SqlDataSource.Relations collection:

using DevExpress.DataAccess.Sql;
using DevExpress.DataAccess.ConnectionParameters;
// ...
void AddQueryRelations()
{
    SelectQuery categories = SelectQueryFluentBuilder
        .AddTable("Categories")
        .SelectAllColumns()
        .Build("Categories");
    SelectQuery products = SelectQueryFluentBuilder
        .AddTable("Products")
        .SelectAllColumns()
        .Build("Products");

    DataSource.Queries.AddRange(new SqlQuery[] { categories, products });
    DataSource.Relations.Add(
        new MasterDetailInfo("Categories", "Products", "CategoryID", "CategoryID"));
}

Call the SqlDataSource.RebuildResultSchema method if you modify the Queries collection.

You can visualize the relationship in the Master-Detail Relation Editor. For this, reference the DevExpress.DataAccess.v23.2.UI.dll assembly and call the SqlDataSourceUIHelper.ManageRelations method or the SqlDataSource.ManageRelations extension method:

using DevExpress.DataAccess.UI.Sql;
// ...
SqlDataSourceUIHelper.ManageRelations(sqlDataSource1);
// You can use an extension method instead:
//sqlDataSource1.ManageRelations();

The invoked dialog is shown in the image below:

Master-Detail Relation Editor