# Automatically Generated Columns

The treelist allows you to automatically generate a column for each public property of its model rather than defining each column manually.

To enable Automatic Column Generation, set the `AutoGenerateColumns` parameter of the treelist to `true`.

The content of this article will be separated into groups for clarity:

* [Basics](https://www.telerik.com/blazor-ui/documentation/components/treelist/columns/auto-generated#basics)
  * [Declare Explicit Columns](https://www.telerik.com/blazor-ui/documentation/components/treelist/columns/auto-generated#declare-explicit-columns)
* [Customization](https://www.telerik.com/blazor-ui/documentation/components/treelist/columns/auto-generated#customization)
  * [Example](https://www.telerik.com/blazor-ui/documentation/components/treelist/columns/auto-generated#example)
* [Notes](https://www.telerik.com/blazor-ui/documentation/components/treelist/columns/auto-generated#notes)

## Basics

To display all model fields in the treelist, just set its `AutoGenerateColumns` parameter to `true`.

If you don't need explicitly declared columns (such as a command column or frozen columns) the `TreeListColumns` tag is not required.

> note The auto-generated columns are not `Expandable` and the treelist will look like a grid. See the [Customization](https://www.telerik.com/blazor-ui/documentation/components/treelist/columns/auto-generated#customization) section below to see how to set up at least one expandable column.

> caption Use the AutoGenerateColumns parameter to generate columns out of a model

<demo metaUrl="client/treelist/columns/auto-generated/example-1/" height="720"></demo>

### Declare Explicit Columns

The treelist can consist of both Automatically Generated and explicitly declared columns.

The following examples show how you can control their order and positions:

* [Default Column Order](https://www.telerik.com/blazor-ui/documentation/components/treelist/columns/auto-generated#default-column-order)
* [Define Explicit Column Order](https://www.telerik.com/blazor-ui/documentation/components/treelist/columns/auto-generated#define-explicit-column-order)

### Default Column Order

By default the Automatically Generated Columns are rendered after the manually declared ones.

> caption Observe the default positioning of the Automatically Generated Columns

<demo metaUrl="client/treelist/columns/auto-generated/example-2/" height="720"></demo>

### Define Explicit Column Order

To set explicit position of the Automatically Generated Columns you can use the `TreeListAutoGeneratedColumns` tag inside the `TreeListColumns` tag and place it in the order you want it to have in relation to the columns you declare.

> note When the `TreeListAutoGeneratedColumns` tag is used the `AutoGenerateColumns` parameter of the treelist must also set to `true`.

> caption Observe explicitly set position of the Automatically Generated Columns between a Checkbox and Command columns

<demo metaUrl="client/treelist/columns/auto-generated/example-3/" height="720"></demo>

## Customization

You can use data annotation attributes to control the column generation, in addition to the options explained above:

* You can set the `Title` of the column through the `[Display(Name="The Desired Title")]` attribute on the fields of your model.

* You can set the `[Display(AutoGenerateField=false)]` attribute over a property of your model if you want to prevent auto-generation of a column from that field.

* You can prevent data mutation by setting the `[Editable]` attribute to `false` on fields of your model.

* The properties that do not have a `setter` or the `setter` is not accessible (`private`) will not be editable either.

* You can set custom width to all auto-generated columns through the `ColumnWidth` parameter of `TreeListAutoGeneratedColumns`. By default they will be equally distributed to fill the width of the treelist.
  * To enable horizontal scrolling, the `ColumnWidth` parameter has to be set so that the sum of all columns is greater than the treelist width. You can find more information in the [Column Width](https://www.telerik.com/blazor-ui/documentation/components/treelist/columns/width) article.

> tip To use the attributes listed above the `System.ComponentModel.DataAnnotations` using statement has to be present in the file with your model. With [Popup editing](https://demos.telerik.com/blazor-ui/treelist/editing-popup), you can use validation attributes in addition to the customization attributes.

### Example

This example shows how to:

* Define an expandable column to showcase hierarchy (define it explicitly, and skip it from column generation through the `[Display(AutoGenerateField = false)]` attribute).
* Use data annotation attributes to
  * set the column titles (e.g., `Email`)
  * make a field non-editable (e.g., `Id`)
  * hide a field from the treelist (e.g., `ParentId`)
  * validate the data (e.g., `Name`)
* Use the `[Display(AutoGenerateField = false)]` attribute to prevent a column from being generated, so you can declare it yourself and customize it (e.g., `Name`)
* Select the order of the columns and set explicit position of the autogenerated Columns
* Make CUD operations (only basic Editing is implemented here for brevity)

> caption Observe the behavior of auto-generated columns with some customizations.

<demo metaUrl="client/treelist/columns/auto-generated/example-4/" height="720"></demo>

## Notes

* When the component initializes, it expects to know what columns to render before the data loads.

If the component is bound to a collection of dynamic objects (such as `Dictionary` or `ExpandoObject`), the component cannot know what columns to render, due to the nature of these objects. Therefore, the `AutoGenerateColumns` feature is not applicable in such scenarios. Columns must be declared explicitly or with a loop. Examples of such setups are available at [Binding Grid to ExpandoObject](https://www.telerik.com/blazor-ui/documentation/knowledge-base/grid-binding-to-expando-object).

## See Also

* [Column Width](https://www.telerik.com/blazor-ui/documentation/components/treelist/columns/width)
* [Live Demo: Auto Generated Columns](https://demos.telerik.com/blazor-ui/treelist/column-auto-generation)
* [Selection](https://www.telerik.com/blazor-ui/documentation/components/treelist/selection/overview)
* [Paging](https://www.telerik.com/blazor-ui/documentation/components/treelist/paging)
