Skip to main content
A newer version of this page is available. .

Unbound Columns

  • 4 minutes to read

Unbound columns are grid columns that display custom data or data calculated from other columns’ values.

Foreword

Unbound columns and regular bound columns are objects of the same classes: 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, requiring you to assign one even if you only need unbound columns. 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 - must be set to a unique value that does not match any data source field name;
  • GridColumn.UnboundType - change this property’s value to the type of data your column should display. Columns automatically use in-place editors that correspond to this type of data.

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",
    UnboundType = DevExpress.Data.UnboundColumnType.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

Your end-users can right-click an unbound column’s header and select “Expression Editor…” to modify the expression. You may choose whether end-users work with a legacy Editor (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

Note

Related API * GridColumn.ShowUnboundExpressionMenu - specifies whether or not end-users can modify an expression for this column.

Demo: Unbound Expressions

Editable Unbound Columns with Custom Data

Handle the ColumnView.CustomUnboundColumnData event to supply unbound columns with completely unique data, rather than data calculated by expressions. This also allows such columns to remain editable. Event arguments provide two parameters for distinguishing between retrieving and editing data.

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


// Creating an unbound column that supports editing
GridColumn unboundColumn = gridView.Columns.AddField("CustomData");
unboundColumn.UnboundType = UnboundColumnType.String;
unboundColumn.Visible = true;
// Handling 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();
        }
    }
};
See Also