Skip to main content

Tutorial: Working with Columns in Code

  • 3 minutes to read

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.

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.


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.

gridControl.DataSource = dimProductBindingSource;

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


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.

gridView1.OptionsBehavior.AutoPopulateColumns = false;
gridControl.DataSource = dimProductBindingSource;

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

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.

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.

GridColumn myCol = new GridColumn() { Caption = "My Column", Visible = true, FieldName = "DealerPrice" };

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

myCol.VisibleIndex = 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.

gridView1.Columns[gridView1.Columns.Count - 1].DisplayFormat.FormatType = gridView1.Columns["ListPrice"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
gridView1.Columns[gridView1.Columns.Count - 1].DisplayFormat.FormatString = gridView1.Columns["ListPrice"].DisplayFormat.FormatString = "C2";


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.

myCol.AppearanceHeader.ForeColor = Color.Crimson;
myCol.AppearanceCell.BackColor = Color.LightGoldenrodYellow;

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


See Also