Create Dynamic Cosmos DB Grids Using the Infragistics XamDataGrid

Ready to get started?

Download a free trial:

Download Now

Learn more:

Cosmos DB ADO.NET Provider

Rapidly create and deploy powerful .NET applications that integrate with Cosmos DB document databases.



Use the CData ADO.NET Provider for Cosmos DB and the Infragistics XamDataGrid to build dynamic grids.

The Infragistics WPF UI controls allow you to build modern, Microsoft Office-inspired apps for desktop and touch devices. When paired with the CData ADO.NET Provider for Cosmos DB, you can build dynamic grids, charts, and other visualizations with access to live Cosmos DB data. This article walks through creating a dynamic grid in Visual Studio using the Infragistics XamDataGrid control.

You will need to install the Infragistics WPF UI components to continue. Download a free trial here: https://www.infragistics.com/products/wpf.

Create a WPF Project

Open VisualStudio and create a new WPF project.

Add a TextBox for passing a SQL query to the CData ADO.NET Provider and a Button for executing the query.

The XAML at this stage is as follows:

< Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:CDataXamDataGridGroupingListApp"
        xmlns:igWPF="http://schemas.infragistics.com/xaml/wpf" x:Class="CDataXamDataGridGroupingListApp.MainWindow"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
  < Grid>
    < TextBox x:Name="textBox" HorizontalAlignment="Left" Height="44" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="682" Margin="10,10,0,0"/>
    < Button x:Name="button" Content="Execute" HorizontalAlignment="Left" Margin="697,10,0,0" VerticalAlignment="Top" Width="85" Height="44"/>
  < /Grid>
< /Window>

Add and Configure a XamDataGrid

After adding the initial controls, add a XamDataGrid to the App. The component will appear in the Visual Studio toolbox.

Arrange the component on the designer so that it is below the TextBox & Button and linked to the boundaries of the app.

Once the XamDataGrid is placed, edit the XAML to set the XamDataGrid DataSource attribute to "{Binding}" and set the FieldSettings AllowRecordFiltering and AllowSummaries attributes to "true." Next, add an empty method as the Click event handler for the Button component. The XAML at this stage is as follows:

< Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:CDataXamDataGridGroupingListApp"
        xmlns:igWPF="http://schemas.infragistics.com/xaml/wpf" x:Class="CDataXamDataGridGroupingListApp.MainWindow"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
  < Grid>
    < TextBox x:Name="textBox" HorizontalAlignment="Left" Height="44" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="682" Margin="10,10,0,0"/>
    < Button x:Name="button" Content="Execute" HorizontalAlignment="Left" Margin="697,10,0,0" VerticalAlignment="Top" Width="85" Click="Button_Click" Height="44"/>

    < igWPF:XamDataGrid Margin="10,59,10,10" DataSource="{Binding}">
      < igWPF:XamDataGrid.FieldSettings>
        < igWPF:FieldSettings AllowSummaries="True" AllowRecordFiltering="True"/>
      < /igWPF:XamDataGrid.FieldSettings>
    < /igWPF:XamDataGrid>

  < /Grid>
< /Window>

Connect to and Query Cosmos DB

The last step in building our WPG App with a dynamic DataGrid is connecting to and querying live Cosmos DB data. First add a reference to the CData ADO.NET Provider to the project (typically found in C:\Program Files\CData\CData ADO.NET Provider for Cosmos DB\lib).

Next, add the Provider to the namespace, along with the standard Data library:

using System.Data.CData.CosmosDB;
using System.Data;

Finally, add the code to connect to Cosmos DB and query using the text from the TextBox to the Click event handler.

To obtain the connection string needed to connect to a Cosmos DB account using the SQL API, log in to the Azure Portal, select Azure Cosmos DB, and select your account. In the Settings section, click Connection String and set the following values:

  • AccountEndpoint: The Cosmos DB account URL from the Keys blade of the Cosmos DB account
  • AccountKey: In the Azure portal, navigate to the Cosmos DB service and select your Azure Cosmos DB account. From the resource menu, go to the Keys page. Find the PRIMARY KEY value and set AccountKey to this value.
private void Button_Click(object sender, RoutedEventArgs e)
{
  //connecting to Cosmos DB
  string connString = "AccountEndpoint=myAccountEndpoint;AccountKey=myAccountKey;";
  using (var conn = new CosmosDBConnection(connString))
  {
    //using the query from the TextBox
    var dataAdapter = new CosmosDBDataAdapter(textBox.Text, conn);
    var table = new DataTable();
    dataAdapter.Fill(table);
    
    //passing the DataRowCollection to the DataContext
    //  for use in the XamDataGrid
    this.DataContext = table.Rows;
  }
}

Run the Application

With the app fully configured, we are ready to display Cosmos DB data in our XamDataGrid. When you click "Execute," the app connects to Cosmos DB and submits the SQL query through the CData ADO.NET Provider.

Live Cosmos DB data is displayed in the grid.

Group the data by dragging and dropping a column name into the header.

As you add groupings and filters, the underlying SQL query is submitted directly to Cosmos DB, making it possible to drill down into live Cosmos DB data to find only the specific information you need.

Free Trial & More Information

At this point, you have created a dynamic WPF App with access to live Cosmos DB data. For more information, visit the CData ADO.NET Provider page. Download a free, 30-day trial and start working live Cosmos DB data in apps built using the Infragistics UI controls today.