Log In
[Expand]General Information
[Collapse]WinForms Controls
 [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
   [Expand]Getting Started
   [Expand]Binding to Data
   [Collapse]Grid View
    [Collapse]Columns and Card Fields
     [Expand]Customization Form
      Tutorial: Column Resizing
      Tutorial: Best Fit Option
      Tutorial: Auto Width Mode
      Tutorial: Fixed Columns
      Tutorial: Column Reordering
      Tutorial: Column Visibility
      Tutorial: Column Chooser API
      Column and Card Field Overview
      Data Cells
      Column Width Options - Auto-Width, Best Fit and Auto-Fill Column
      Column Visibility
      Creating Columns and Binding Them to Data Fields
      Fixed Columns
    [Expand]Rows and Cards
    [Expand]Row, Column and Cell Access API
    [Expand]Split Presentation
    [Expand]Cell Merging
    [Expand]Row Preview Sections
   [Expand]View Technology
   [Expand]Data Editing
   [Expand]Filtering and Locating Rows
   [Expand]Focus and Selection Handling
   [Expand]Processing Rows
   [Expand]Formatting Cell Values
   [Expand]Master-Detail Relationships
   [Expand]Asynchronous Image Load
   [Expand]Export and Printing
   [Expand]Appearance and Conditional Formatting
   [Expand]Batch Modifications
   [Expand]Hit Information
   [Expand]Popup Menus
   [Expand]Saving and Restoring Layouts
   [Expand]Visual Elements
   [Expand]Design-Time Features
   [Expand]End-User Capabilities
    Included Components
  [Expand]Vertical Grid
  [Expand]Pivot Grid
  [Expand]Tree List
  [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]Common Features
  Get More Help
 [Expand]API Reference
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Document Server
[Expand]Report Server
[Expand]eXpressApp Framework
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation

Creating Columns and Binding Them to Data Fields

Views display data in columns. So, when your grid control is bound to a data source, you need to create columns within Views and bind them to data fields. Otherwise, Views will not display data.

Expanded Column Creation and Binding Basics

All Views available in Grid Control are derived from the ColumnView class. This class implements the basic column functionality and is extended by descendants as required. This class declares the ColumnView.Columns property, which is a column collection owned by a View. The Columns property is overridden by the BandedGridView and LayoutView classes, since these classes use columns represented by BandedGridColumn and LayoutViewColumn objects respectively. Columns in the GridView and CardView Views are represented by GridColumn objects. See Column and Card Field Overview for details.

There are two ways to add columns and bind them to fields.

  • Create columns and bind them to fields manually.

    You can use methods of the collection object returned by the View's Columns property to add, delete and access individual columns. After a column is created and added to a collection, it can be bound to a data field using the GridColumn.FieldName property. This property provides the name of the data field bound to the column. If a View is bound to data, as described in the Binding Controls to Data Created at Runtime topic, the property specifies the name of a record object's public property. At design time, you can access the Columns collection using an appropriate designer page. Please refer to the next section for details.

  • Automatically create columns for all fields within a bound data source.

    Views allow you to populate a Columns collection with columns for all data fields at once. At runtime, this is performed by calling a View's ColumnView.PopulateColumns method. The next section describes how to do this at design time. Note that if a View contains no columns on application start-up, the ColumnView.PopulateColumns method is called for the View automatically.

The two methods mentioned above can be used in combination. For instance, you may want to display all data fields except one. In this case, the easiest way is to populate the full column collection automatically, then delete the unwanted column.

When adding a column to a Card View, Grid View or Banded Grid View at design time, the column is displayed immediately. However, when adding columns at runtime via the Add method, they are initially hidden. You can make a column visible later via the GridColumn.VisibleIndex property. To create an initially visible column, use the GridColumnCollection.AddVisible method. End-users can hide columns or make them visible using the Customization Form.

Advanced Banded Grid View columns have different rules. They are also not displayed immediately at design time. To show these columns at design time, you need to use the designer's Band Designer page to make them visible, by dragging them to an appropriate band. At runtime, you will need to set a column's BandedGridColumn.OwnerBand property to the desired band and call a View's AdvBandedGridView.SetColumnPosition method.

A Layout View behaves like other Views. Columns created at design time are automatically made visible, while columns created in code are initially hidden. To specify the layout of columns at design time, use the designer's Layout Page. At runtime, to position a column within a template card represented by a LayoutView.TemplateCard property, use the column's Visible property or methods provided by a corresponding LayoutViewColumn.LayoutViewField object.

If a column's GridColumn.FieldName property contains an empty string or doesn't match any of the field names in the associated data source, the column is not bound to a data field. Column cells of this kind are always empty. They can be edited, but the value typed disappears when editing is finished. Note, however, that such columns can also be used for display purposes. You can make this column read-only and display information within its cells using custom painting. In other words, you will need to disable the column's OptionsColumn.AllowEdit option and handle the View's GridView.CustomDrawCell or CardView.CustomDrawCardFieldValue event (depending on the View type).

Expanded Creating and Binding Columns at Design Time

At design time, you can access the Columns collection via the designer's Columns Page. It allows you to manipulate columns (create and remove columns, retrieve columns from a bound data source and access column settings). After columns have been created, you can customize their settings. In the designer's Columns Page, select a specific column to display its settings in the property grid.

The Grid Control provides on-form access to the settings of columns and bands. If a column/band is visible at design time, you do not need to activate the designer to gain access to its settings. Instead, you can click the column's/band's header right on the form to list the object's properties and events in the Properties grid. See On-form Band and Column Customization for more information.

Expanded Creating and Binding Columns at Runtime

This example shows you how to add columns using both methods previously discussed. The code first shows a message box displaying the question "Do you want to create columns for all fields?". If the Yes button is pressed, the View's ColumnView.PopulateColumns method is called. Otherwise, columns for the ProductID, ProductName, QuantityPerUnit and UnitPrice fields are created using the Columns collection methods.

Expanded See Also

How would you rate this topic?​​​​​​​