Skip to main content

Unbound Columns

  • 5 minutes to read

Unbound columns are Grid columns that display any custom data. You can utilize unbound columns to show data from external sources, combine multiple data sources into one, or use expressions to calculate values based on other column data.

Watch Video

Foreword

Unbound columns and regular bound columns are objects of the same class: the GridColumn, BandedGridColumn, or LayoutViewColumn class. This means unbound columns fully support all column features — sorting, grouping, filtering, summaries, etc.

However, the Data Grid cannot operate without a data source, even if all of your columns are unbound. For example, utilize the UnboundSource component to tell the Data Grid how many rows it should generate.

Create Unbound Columns

To add unbound columns, invoke the Data Grid Designer’s “Columns” tab and click the “Add Column” button. In the property grid, set up two properties:

  • GridColumn.FieldName — You must set this property to a unique value that does not match any data source field name.
  • GridColumn.UnboundDataType — Set this property to the type of data your column should display. Columns automatically use in-place editors that correspond to the selected data type.

Data Grid - Unbound Columns - Designer

If you add unbound columns in code, remember to explicitly set their visibility.

gridView1.Columns.Add(new DevExpress.XtraGrid.Columns.GridColumn() {
    Caption = "Custom Unbound Column",
    FieldName = "UnboundColumn3",
    UnboundDataType = typeof(string),
    Visible = true });

Unbound Expressions

Unbound expressions allow you to calculate values of unbound columns based on other column values. Expressions are built in the Expression Editor dialog. To invoke this dialog, click the ellipsis button next to the GridColumn.UnboundExpression property in the VS Property Grid.

Data Grid - Unbound Columns - DT Expression Editor

In code, expressions are assigned as simple strings. For instance, the code below calculates the year by subtracting values of the grid column with the “Int” field name from the current year.

gridColumn3.UnboundExpression = "GetYear(AddYears(LocalDateTimeNow(), - [Int]))",

Tip

Unbound columns that utilize expressions remain editable, although since cell values are calculated according to an expression, edits made by end-users are automatically discarded. It is recommended that you disable the OptionsColumn.AllowEdit setting for such columns.

Demo: Unbound Expressions

Modify Unbound Expressions at Runtime

End users can right-click an unbound column’s header and select “Expression Editor…” to modify the expression. You may choose whether users work with a legacy Editor (the same one you use at design time), or its updated version that supports syntax highlighting and auto-completion.

Data Grid - Unbound Columns - RT Expression Editor

Related API

Demo: Unbound Expressions

Editable Unbound Columns with Custom Data

The ColumnView.CustomUnboundColumnData event is a single entry point for two tasks: it allows you to populate cells with values, and obtain user edits (which you can accept or discard). Read values of the IsGetData and IsSetData properties to specify a different set of actions for each of these scenarios.

The Unbound Columns Code Example illustrates how to retrieve data from a Dictionary and save changes back to it.

// Create an unbound column that supports editing
GridColumn unboundColumn = gridView.Columns.AddField("CustomData");
unboundColumn.UnboundDataType = typeof(string);
unboundColumn.Visible = true;
// Handle the CustomUnboundColumnData event
Dictionary<int, string> unboundData = new Dictionary<int, string>();
unboundData[1] = "Can live up to 20 years!";
gridView.CustomUnboundColumnData += (sender, e) =>
{
    if(e.Column.FieldName == "CustomData") {
        // Populate columns
        if(e.IsGetData) {
            if(unboundData.ContainsKey(e.ListSourceRowIndex)) 
                e.Value = unboundData[e.ListSourceRowIndex];
        }
        // Post edits back to the source
        if(e.IsSetData && e.Value != null) {
            unboundData[e.ListSourceRowIndex] = e.Value.ToString();
        }
    }
};

Refresh Unbound Columns

Data-aware controls do not cache values in unbound columns/rows, and request these values every time a control is painted, filtered, sorted, etc. You can use this behavior to trigger an update and reload unbound values.

Cheat Sheets and Best Practices

Read the following quick-reference guide for general information and examples:

Show Values from External Sources. Calculated Field Values. Unbound Mode.

See Also