Skip to main content

Grid Columns

  • 7 minutes to read

Important Notes

Automatic Column Generation

When you bind a Data Grid to a data source, the control automatically generates a column for each bound data field (if the AutoPopulateColumns property is not disabled). For code-first data sources (for example, Entity Framework models), you can mark a property with the Display attribute and disable its AutoGenerateField parameter to skip column generation for this property. The “Display” attribute in the code below prevents the Data Grid from generating an “Additional Info” column.

[Display(AutoGenerateField = false, Description = "This column isn't created")]
public string AdditionalInfo { get; set; }

You can utilize many more attributes to re-arrange columns, modify captions of their headers, change their default in-place editors, etc. Refer to the following topic to learn more: Data Annotation Attributes.

If you replaced the previous data source with a new one, the Data Grid will not automatically regenerate columns. You need to do this manually by invoking the Data Grid Designer and clicking the “Retrieve Fields” button within the “Columns” tab.

Data Grid - Retrieve Fields Designer

To do the same in code, call the BaseView.PopulateColumns method either directly after changing a data source, or on the GridControl.DataSourceChanged event.

Note

In addition to regular columns that retrieve data from a source, Grid Control also supports unbound columns that display custom values. See this help topic to learn more: Unbound Columns.

See also: Obtaining Fields Available in Data Source.

Add and Remove Columns Manually

Columns associated with data fields declared in a data source are called bound columns. To add such columns manually, invoke the Data Grid designer, switch to the “Columns” tab and click the “Show Field List” button, then drag a data field into the column list. Data fields that have no corresponding grid columns are highlighted with bold text.

Data Grid - Add Bound Columns in Designer

You can also utilize the “Add Column” and “Insert Column” buttons on the same designer page. In this case, you need to set every column’s GridColumn.FieldName property manually. Otherwise, a column is unbound and does not receive cell values from a data source.

Data Grid - Add Column Button

To remove a column, utilize the “Remove Column” designer button, or click a column header at design time and press the “Delete” key. To remove a column in code, call the GridView.Columns.Remove or GridView.Columns.RemoveAt method.

Columns that you create in code are invisible even after you add them to the Columns collection. To display a column, enable its Visible property. You can also use the AddVisible method to create a column, bind it to the specified data field, and display it in the view.

// Create a new column.
GridColumn colPrice = new GridColumn();
// Bind the column to the 'Price' field in a data source.
colPrice.FieldName = "Price";
// Adds the column to the Columns collection. The column is hidden.
gridView.Columns.Add(colPrice);

// Creates a new column, binds it to the 'OrderDate' field in a data source,
// and displays it in the Grid View.
gridView.Columns.AddVisible("OrderDate");

Column Header

To modify a column header caption and add an image, select a column at design time and invoke its smart tag. If a column has no caption assigned, the column generates its caption based on the name of a related data field.

Data Grid - Column Smart Tag

Related API

Hide Vertical Column Borders

You can hide column and row borders by disabling the GridOptionsView.ShowVerticalLines and GridOptionsView.ShowHorizontalLines settings.

Data Grid - Vertical Lines

Column Width

Data Grid squeezes all its columns into its current client area. Users can resize all columns except for the last one, and the total column width cannot exceed the width of the parent View.

Data Grid - Auto Width

Disable the GridOptionsView.ColumnAutoWidth property to allow grid columns to transcend the View bounds. A horizontal scroll bar is displayed automatically if columns occupy more space than the View provides.

Data Grid - Disabled Auto Width

Related API

Best Fit

To ensure a column or all the View’s columns have enough width for cells to display their content entirely, end users can right-click a column header and choose the “Best Fit” (or “Best Fit (all columns)”) option.

BestFit

Related API

Auto-Fill Column

A grid column assigned to the GridView.AutoFillColumn property automatically resizes to fill in any free space a View provides. In the animation below, the auto-fill column is “Address”.

Grid - Auto Fill Column

Fixed Columns

Set a column’s Fixed property to Left or Right to anchor that column to the View’s corresponding side. Fixed columns remain visible while a user scrolls the View horizontally.

Data Grid - Fixed Columns Animation

If you set the Fixed property to MiddleLeft, the column is not anchored until a user scrolls it to the View’s left side. When the column reaches the left side, it becomes fixed while other columns continue to scroll.

Data Grid - Dynamically Fixed Columns Animation

Use the FixedColumnHighlightMode property to distinguish the fixed columns from regular columns.

The Fixed Columns demo illustrates how to supply grid columns with custom pop-up menus that allow users to anchor columns at runtime.

Related API

End-User Capabilities

By default, end users can do the following:

Identify and Access Grid Columns in Code

To retrieve specific columns, utilize the following API:

See Also