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 found data field (if the AutoPopulateColumns property is not disabled). For code-first data sources (e.g., Entity Framework models), you can mark a property with the Display attribute and disable its AutoGenerateField parameter to skip generating a column for this property. The “Display” attribute in the code below prevents a 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 Data Annotation Attributes article to learn more.

In case you have replaced a previous data source with a new one, the Data Grid will not automatically re-generate columns. You need to do that 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 Columns Designer Buttons

To remove a column, utilize the “Remove Column” designer button, or click a column header at design time and press the “Delete” key.

Columns that you create in code are invisible even after you add them to the Columns collection. To display a column, either enable its Visible property…

GridColumn colPrice = new GridColumn();
colPrice.FieldName = "Price";
colPrice.Visible = true; //set this property to display a column
gridView.Columns.Add(colPrice);

… or use the AddVisible method instead of regular Add or Insert.

gridView.Columns.AddVisible("Discount");

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

By default, the Data Grid squeezes all its columns into its current client area. End-users can resize columns except for the last one and total column width cannot exceed the width of a 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 columns) has enough width for its cells to entirely display their content, 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 at 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 up 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

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