[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: Working with Columns in Code

This walkthrough is a transcript of the Working with Columns in Code video available on the DevExpress YouTube Channel.

In this tutorial, you will learn how to enable or disable automatic column generation when assigning a data source, how to manually populate the column collection and access individual columns. Note that this tutorial focuses on accomplishing these tasks in code. You can obviously do the same using the grid's integrated Designer dialog and the Visual Studio's Property grid, which is described in a separate tutorial.

Expanded Starting Point

The application initially contains a grid control that doesn't have any columns and is not bound to a data source. The project has a connection to the sample AdventureWorks database. You can see it available in the grid control's Data Source Wizard.

Expanded Automatically Retrieving Data Source Fields

Instead of binding the grid to data using the Wizard, switch to code view and set the GridControl.DataSource property in the Form's constructor.

Run the application to see that columns are automatically generated for every available field in the bound data source.

Expanded Disabling Automatic Column Generation

If you need to only bind the control to data, without automatically generating all columns, set the ColumnViewOptionsBehavior.AutoPopulateColumns property to false.

If you run the app now, you can see a blank grid control again, despite the fact that it has a bound data source.

Expanded Manually Populating Columns

Grid Views provide a method that deletes all previously created columns and creates one for each data source field, much like the behavior you witnessed with the ColumnViewOptionsBehavior.AutoPopulateColumns option turned on.

Run the application to see the column back in the view again.

Expanded Modifying Columns Collection

All grid control columns are GridColumn class instances, stored within the View's ColumnView.Columns collection. You are free to modify this collection as needed. For instance, you can remove the column that was automatically generated for the DealerPrice field.

Launch the app to make sure the column for this field is not there anymore.

Now create a new column bound to the DealerPrice field.

To move the newly created column to the first position within the view, set its GridColumn.VisibleIndex property to 0.

Run the application to check that the position has in fact changed. Notice that the added column's formatting has not been properly set to display currency symbol. The same is true for the ListPrice column.

Take a look at two different ways to access an individual column. One is by index in the collection, since you know that the column you added in code is the last one. Another is by the field name and this is how you're going to access the ListPrice column. Set both column FormatInfo.FormatType and FormatInfo.FormatString properties to apply proper formatting.

Finally, change the appearance of the column created in code. As you can see, you can still modify the same column object that you added to the ColumnView.Columns collection.

Run the application and see the change in appearance settings applied to only one column.

Expanded See Also

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