[Expand]General Information
[Collapse]WinForms Controls
  .NET Core Support
 [Expand]What's Installed
 [Expand]Build an Application
 [Collapse]Controls and Libraries
  [Expand]Forms and User Controls
  [Expand]Messages, Notifications, and Dialogs
  [Expand]Editors and Simple Controls
  [Expand]Ribbon, Bars and Menu
  [Expand]Application UI Manager
  [Expand]Docking Library
  [Collapse]Data Grid
   [Collapse]Get Started With Data Grid and Views
     [Expand]Appearance and Conditional Formatting
     [Expand]Data Editing
     [Expand]Hit Information
     [Collapse]Data Binding and Working with Columns
       Tutorial: ADO.NET Data
       Tutorial: Working with Columns in Code
       Tutorial: Unbound Columns
       Tutorial: Create and Manage Columns at Design Time
       Tutorial: Entity Framework Data
       Tutorial: Create and Manage Data in Code and Apply Data Annotation Attributes
       Tutorial: Large Data Sources and Instant Feedback with Server Mode
     [Expand]Grid View - Columns, Rows and Cells
     [Expand]Banded Views
     [Expand]WinExplorer View
     [Expand]Tile View
     [Expand]Filter and Search
     [Expand]Split Presentation
     [Expand]Row Preview Sections
   [Expand]Data Binding
    Unbound Columns
   [Expand]Data Editing and Validation
   [Expand]Filter and Search
   [Expand]Focus and Selection Handling
    Format Cell Values
   [Expand]Master-Detail Relationships
   [Expand]Asynchronous Image Load
   [Expand]Export and Printing
   [Expand]Appearance and Conditional Formatting
    Split Presentation
    Row Preview Sections
   [Expand]Batch Modifications
    Hit Information
    Popup Menus
   [Expand]Save and Restore Layout
   [Expand]Visual Elements
   [Expand]Design-Time Features
   [Expand]End-User Capabilities
    Included Components
  [Expand]Vertical Grid
  [Expand]Property Grid
  [Expand]Pivot Grid
  [Expand]Tree List
  [Expand]Gantt Control
  [Expand]Chart Control
  [Expand]Map Control
  [Expand]Rich Text Editor
  [Expand]Spell Checker
  [Expand]Form Layout Managers
  [Expand]Navigation Controls
  [Expand]PDF Viewer
  [Expand]TreeMap Control
  [Expand]Sunburst Control
 [Expand]Common Features
  Get More Help
 [Expand]API Reference
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Office File API
[Expand]Report and Dashboard Server
[Expand]eXpressApp Framework
[Expand]eXpress Persistent Objects
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation
View this topic on docs.devexpress.com (Learn more)

Tutorial: Unbound Columns

This walkthrough is a transcript of the Unbound Columns video available on the DevExpress YouTube Channel.

In this tutorial, you will learn how to add columns whose values are not stored in the data source and are calculated using a pre-defined expression or specified with an event handler. You'll also see how you can provide separate storage for values displayed in these unbound columns.

Data for the unbound columns will be calculated based on values of the Quantity, UnitPrice and Discount columns.

Expanded Creating Unbound Columns and Specifying Expressions for them at Design Time

Select a grid control and then click its smart tag. Click Add Column twice to create two new columns.

Resize the columns to make them wider. Click the first created column and set its GridColumn.FieldName to a unique string: "DiscountAmount".

Then, set the column's GridColumn.UnboundType property to any value other than UnboundColumnType.Bound. The column will display decimal values in this case.

Use the GridColumn.UnboundExpression property to specify an expression used to calculate column values. Click the ellipsis button to open the Expression Editor, which will allow you to create formulas using various functions, operators, data field values, and constants. Create a simple expression that multiplies three existing fields.

To make this unbound column read-only, set the OptionsColumn.AllowEdit property to false.

Now format the values of the two columns as currency. Hold down the CTRL or SHIFT key and click another column. This selects two columns simultaneously. In the Properties grid, expand GridColumn.DisplayFormat and set FormatInfo.FormatType to FormatType.Numeric and FormatInfo.FormatString to c2.

Run the application. The first column displays calculated data and formats it as currency.

Expanded Editing Expressions for Unbound Columns at Runtime

It is possible to allow an end-user to modify expressions for unbound columns at runtime. To accomplish this, set the column's GridColumn.ShowUnboundExpressionMenu property to true.

The column's context menu now provides the Expression Editor... command. When it's invoked, the Expression Editor dialog opens allowing you to modify the expression.

Expanded Providing Data for Unbound Columns Using an Event

For the second column, whose data will be provided later using an event, we also need to specify a unique GridColumn.FieldName and set its GridColumn.UnboundType property to UnboundColumnType.Decimal.

Supply values for the second unbound column by handling a dedicated event. Select gridView1 and switch to the Events page in the Properties grid. Subscribe to the ColumnView.CustomUnboundColumnData event.

The ColumnView.CustomUnboundColumnData event fires each time a column value is about to be displayed onscreen, and also after a column cell is modified and data posting is required. Values for the Total column will be calculated from Quantity, UnitPrice and Discount column values. First, you need to retrieve these values using methods of the bound data source. The grid control provides a helper ColumnView.GetListSourceRowCellValue method to do this. Second, calculate a value for the unbound column and assign it to the CustomColumnDataEventArgs.Value event parameter.

Run the application. The Total column displays data provided by the event handler.

This column is not read-only. Therefore, you can modify cells, but the changes will not be saved.

Expanded Implementing Data Posting for Unbound Columns

The data grid does not store or cache values of unbound columns. It's up to you to implement data posting. To illustrate the data posting feature, modify the code.

In this code, values for the Total column are stored in a dictionary. When an end-user modifies a cell, the ColumnView.CustomUnboundColumnData event fires and its CustomColumnDataEventArgs.IsSetData parameter equals to true. The new value is saved in the dictionary for later use.

Run the application again. Try modifying values to confirm that the changes were saved.

Expanded See Also

Is this topic helpful?​​​​​​​