Log In
Home
Support
Demos
Documentation
Blogs
Training
Webinars
[Expand]General Information
[Collapse]WinForms Controls
  Prerequisites
 [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
   [Collapse]Binding to Data
    [Expand]Server Mode
     Grid Control Binding Specifics
     Posting Data to a Connected Database
     Unbound Columns
     Binding to Entity Framework Models
     Binding via Data Source Configuration Wizard
     Fluent API Support
     Member Table: Data Binding
     Tutorial: ADO.NET Data
     Tutorial: Create and Manage Columns at Design Time
     Tutorial: Create and Manage Data in Code and Apply Data Annotation Attributes
     Tutorial: Entity Framework Data
     Tutorial: Unbound Columns
     Tutorial: Working with Columns in Code
   [Expand]Grid View
   [Expand]View Technology
   [Expand]Grouping
   [Expand]Sorting
   [Expand]Summaries
   [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]Hints
   [Expand]Popup Menus
   [Expand]Saving and Restoring Layouts
   [Expand]Visual Elements
   [Expand]Design-Time Features
   [Expand]Examples
   [Expand]End-User Capabilities
    Included Components
  [Expand]Vertical Grid
  [Expand]Pivot Grid
  [Expand]Tree List
  [Expand]Chart Control
  [Expand]Diagrams
  [Expand]Gauges
  [Expand]Map Control
  [Expand]Scheduler
  [Expand]Spreadsheet
  [Expand]Rich Text Editor
  [Expand]Spell Checker
  [Expand]Form Layout Managers
  [Expand]Navigation Controls
  [Expand]Printing-Exporting
  [Expand]PDF Viewer
   Reporting
  [Expand]Snap
  [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]Reporting
[Expand]Report Server
[Expand]Dashboard
[Expand]eXpressApp Framework
[Expand]CodeRush
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation

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

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