Unbound Columns
- 4 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.
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.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.
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.
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.
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.
Related API
GridColumn.ShowUnboundExpressionMenu - specifies whether or not end-users can modify an expression for this column.
ColumnViewOptionsBehavior.UnboundColumnExpressionEditorMode - gets or sets which Expression Editor end-users utilize.
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.
- UnboundColumnDataEventArgs.IsGetData - equals true when the column needs to be populated and you need to provide cell values.
- UnboundColumnDataEventArgs.IsSetData - equals true when an end-user has changed a cell value and you need to save these edits to a source.
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.UnboundDataType = typeof(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();
}
}
};
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.
To reload all currently visible values, call the BaseView.LayoutChanged method that refreshes an entire View.
To reload values of a specific row or cell, call the ColumnView.RefreshRow or GridView.RefreshRowCell method respectively.