Skip to main content

Unbound Columns

  • 6 minutes to read

Bound and Unbound Columns

The Grid Control supports bound and unbound columns. Bound columns obtain their data from data fields in a grid’s data source. Unbound columns are not bound to any field in the data source. There is no difference between working with bound and unbound columns. You can sort, group, display summaries and filter unbound columns in the same manner as bound columns.

An unbound column, however, is one that meets the following two requirements:

  • The ColumnBase.FieldName property must be set to a unique value, and not refer to any field in the grid’s data source.
  • The ColumnBase.UnboundType property must be set to an appropriate value according to the type of data this column should display (Boolean, Bound, DateTime, Decimal, Integer, String or Object).

Note

If unbound data is obtained from a custom data source, when adding a new row you should add a new entry to the custom data source that corresponds to the new record in the grid. Similarly, when a record is deleted, delete the corresponding entry in the custom data source. To receive notifications that a record has been added or removed, use the methods provided by the data source.

Note

The Grid Control cannot operate with only unbound columns. It must be bound to a data source using its DataControlBase.ItemsSource property.

Provide Data for Unbound Columns

Unbound columns must be populated manually. You can use the ColumnBase.UnboundExpression property and specify an expression used to evaluate values for an unbound column based on the items source records.

The sample code below shows how to specify an unbound expression to display the total price, calculated as: UnitPrice * Quantity:

<grid:GridColumn FieldName="Total" UnboundType="Decimal" UnboundExpression="[UnitPrice] * [Quantity]"/>

If data for unbound columns is obtained from a custom data source, handle the GridControl.CustomUnboundColumnData event.

Example: Display Unbound Data

This example shows how to add an unbound column to the Grid Control.

<Page
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Unbound_Columns"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:Grid="using:DevExpress.UI.Xaml.Grid"
    x:Class="Unbound_Columns.MainPage"
    mc:Ignorable="d">
    <Grid>
        <Grid:GridControl Name="grid" AutoGenerateColumns="False" CustomUnboundColumnData="grid_CustomUnboundColumnData" ShowFixedTotalSummary="True" >
            <Grid:GridControl.Columns>
                <Grid:GridTextColumn FieldName="ProductName" />
                <Grid:GridTextColumn FieldName="UnitPrice"/>
                <Grid:GridTextColumn FieldName="Quantity" />
                <Grid:GridTextColumn FieldName="Total" UnboundType="Decimal" />
            </Grid:GridControl.Columns>
        </Grid:GridControl>
    </Grid>
</Page>
using System;
using DevExpress.Data;
using Windows.UI.Xaml.Controls;

namespace Unbound_Columns {
    public sealed partial class MainPage : Page {
        public MainPage() {
            this.InitializeComponent();
            grid.ItemsSource = new ProductList();
            grid.TotalSummary.Add(SummaryItemType.Count, "ProductName");
            grid.TotalSummary.Add(SummaryItemType.Max, "UnitPrice").DisplayFormat = "Max: {0:c2}";
            grid.TotalSummary.Add(SummaryItemType.Average, "UnitPrice").DisplayFormat = "Avg: {0:c2}";
        }

        private void grid_CustomUnboundColumnData(object sender, DevExpress.UI.Xaml.Grid.GridColumnDataEventArgs e) {
            if (e.IsGetData) {
                int price = Convert.ToInt32(e.GetListSourceFieldValue("UnitPrice"));
                int quantity = Convert.ToInt32(e.GetListSourceFieldValue("Quantity"));
                e.Value = price * quantity;
            }
        }
    }
}
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;

public class Product {
    public string ProductName { get; set; }
    public string Country { get; set; }
    public string City { get; set; }
    public double UnitPrice { get; set; }
    public int Quantity { get; set; }
}

public class ProductList : ObservableCollection<Product> {
    public ProductList()
        : base() {
        Add(new Product() { ProductName = "Chang", Country = "UK", City = "Cowes", UnitPrice = 19, Quantity = 10 });
        Add(new Product() { ProductName = "Gravad lax", Country = "Italy", City = "Reggio Emilia", UnitPrice = 12.5, Quantity = 16 });
        Add(new Product() { ProductName = "Ravioli Angelo", Country = "Brazil", City = "Rio de Janeiro", UnitPrice = 19, Quantity = 12 });
        Add(new Product() { ProductName = "Tarte au sucre", Country = "Germany", City = "QUICK-Stop", UnitPrice = 22, Quantity = 50 });
        Add(new Product() { ProductName = "Steeleye Stout", Country = "USA", City = "Reggio Emilia", UnitPrice = 18, Quantity = 20 });
        Add(new Product() { ProductName = "Pavlova", Country = "Austria", City = "Graz", UnitPrice = 21, Quantity = 52 });
        Add(new Product() { ProductName = "Longlife Tofu", Country = "USA", City = "Boise", UnitPrice = 7.75, Quantity = 120 });
        Add(new Product() { ProductName = "Alice Mutton", Country = "Mexico", City = "México D.F.", UnitPrice = 21, Quantity = 15 });
        Add(new Product() { ProductName = "Alice Mutton", Country = "Canada", City = "Tsawwassen", UnitPrice = 44, Quantity = 16 });
    }
}

Updating Unbound Data

In most instances, the grid updates unbound cells automatically. To force unbound data updates manually, use the DataControlBase.UpdateUnboundColumnValues method. This method updates all unbound cells contained within the specified data row regardless of how unbound data s supplied (via event handler or unbound expression).