---
title: "Column Definitions"
framework: javascript
version: "36.1.0"
---

# Column Definitions

Each column in the grid is defined using a Column Definition (`ColDef`). Columns are positioned in the grid according to the order the Column Definitions are specified in the Grid Options.

#### Simple Definitions

```ts
import {
  ClientSideRowModelModule,
  GridApi,
  GridOptions,
  ModuleRegistry,
  createGrid,
  enableDevValidations,
} from "ag-grid-community";
import { IOlympicData } from "./interfaces";

// Enable extended validations only for development
if (process.env.NODE_ENV !== "production") {
  enableDevValidations();
}

ModuleRegistry.registerModules([ClientSideRowModelModule]);

let gridApi: GridApi<IOlympicData>;

const gridOptions: GridOptions<IOlympicData> = {
  // define grid columns
  columnDefs: [{ field: "athlete" }, { field: "sport" }, { field: "age" }],
};

const gridDiv = document.querySelector<HTMLElement>("#myGrid")!;
gridApi = createGrid(gridDiv, gridOptions);

fetch("https://www.ag-grid.com/example-assets/olympic-winners.json")
  .then((response) => response.json())
  .then((data: IOlympicData[]) => gridApi!.setGridOption("rowData", data));
```

[Live example: Simple Definitions](https://www.ag-grid.com/examples/column-definitions/simple/typescript)

```js
const gridOptions = {
    columnDefs: [
        { field: 'athlete' },
        { field: 'sport' },
        { field: 'age' }
    ],

    // other grid options ...
}
```

See [Column Options](https://www.ag-grid.com/javascript-data-grid/column-properties/) for all available properties.

## Column Defaults

Use `defaultColDef` to set properties across ALL Columns.

```js
const gridOptions = {
    defaultColDef: {
        width: 150,
        cellStyle: { fontWeight: 'bold' },
    },

    // other grid options ...
}
```

#### Default Col Def

```ts
import {
  CellStyleModule,
  ClientSideRowModelModule,
  GridApi,
  GridOptions,
  ModuleRegistry,
  createGrid,
  enableDevValidations,
} from "ag-grid-community";
import { IOlympicData } from "./interfaces";

// Enable extended validations only for development
if (process.env.NODE_ENV !== "production") {
  enableDevValidations();
}

ModuleRegistry.registerModules([CellStyleModule, ClientSideRowModelModule]);

let gridApi: GridApi<IOlympicData>;

const gridOptions: GridOptions<IOlympicData> = {
  // define grid columns
  columnDefs: [{ field: "athlete" }, { field: "sport" }, { field: "age" }],
  defaultColDef: {
    width: 150,
    cellStyle: { fontWeight: "bold" },
  },
};

const gridDiv = document.querySelector<HTMLElement>("#myGrid")!;
gridApi = createGrid(gridDiv, gridOptions);

fetch("https://www.ag-grid.com/example-assets/olympic-winners.json")
  .then((response) => response.json())
  .then((data: IOlympicData[]) => gridApi!.setGridOption("rowData", data));
```

[Live example: Default Col Def](https://www.ag-grid.com/examples/column-definitions/default-col-def/typescript)

## Cell Data Types

The grid provides built-in [Cell Data Types](https://www.ag-grid.com/javascript-data-grid/cell-data-types/) for common data types such as `text`, `number`, `boolean`, `date` and more. By default these types are [inferred](https://www.ag-grid.com/javascript-data-grid/cell-data-types/#inferring-data-types) from the row data and configure appropriate rendering, editing, filtering, and sorting behaviour for each column without the need for explicit configuration via `columnDefs`.

## Column Types

Use `columnTypes` to define a set of Column properties to be applied together. The properties in a column type are applied to a Column by setting its `type` property.

```js
const gridOptions = {
    // Define column types
    columnTypes: {
        currency: {
            width: 150,
            valueFormatter: currencyFormatter
        },
        shaded: {
            cellClass: 'shaded-class'
        }
    },
    columnDefs: [
        { field: 'productName'},

        // uses properties from currency type
        { field: 'boughtPrice', type: 'currency'},

        // uses properties from currency AND shaded types
        { field: 'soldPrice', type: ['currency', 'shaded'] },
    ],

    // other grid options ...
}
```

> **Note**
>
> Column Types work on Columns only and not Column Groups.

The below example shows Column Types.

#### Column Definition Example

```ts
import {
  CellStyleModule,
  ClientSideRowModelModule,
  GridApi,
  GridOptions,
  ModuleRegistry,
  ValueFormatterParams,
  createGrid,
  enableDevValidations,
} from "ag-grid-community";

// Enable extended validations only for development
if (process.env.NODE_ENV !== "production") {
  enableDevValidations();
}

ModuleRegistry.registerModules([CellStyleModule, ClientSideRowModelModule]);

interface SalesRecord {
  productName: string;
  boughtPrice: number;
  soldPrice: number;
}

function currencyFormatter(params: ValueFormatterParams) {
  const value = Math.floor(params.value);
  if (isNaN(value)) {
    return "";
  }
  return "£" + value.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
}

const gridOptions: GridOptions<SalesRecord> = {
  // define column types
  columnTypes: {
    currency: {
      width: 150,
      valueFormatter: currencyFormatter,
    },
    shaded: {
      cellClass: "shaded-class",
    },
  },
  // define grid columns
  columnDefs: [
    { field: "productName" },
    // uses properties from currency type
    { field: "boughtPrice", type: "currency" },
    // uses properties from currency AND shaded types
    { field: "soldPrice", type: ["currency", "shaded"] },
  ],

  rowData: [
    { productName: "Lamp", boughtPrice: 100, soldPrice: 200 },
    { productName: "Chair", boughtPrice: 150, soldPrice: 300 },
    { productName: "Desk", boughtPrice: 200, soldPrice: 400 },
  ],
};

const gridDiv = document.querySelector<HTMLElement>("#myGrid")!;
const gridApi = createGrid(gridDiv, gridOptions);
```

[Live example: Column Definition Example](https://www.ag-grid.com/examples/column-definitions/column-types/typescript)

## Provided Column Types

The grid provides the Column Types `rightAligned` and `numericColumn`. Both of these types right align the header and cell contents by applying CSS classes `ag-right-aligned-header` to Column Headers and `ag-right-aligned-cell` to Cells.

```js
const gridOptions = {
    columnDefs: [
        { headerName: 'Column A', field: 'a' },
        { headerName: 'Column B', field: 'b', type: 'rightAligned' },
        { headerName: 'Column C', field: 'c', type: 'numericColumn' },
    ],

    // other grid options ...
}
```

> **Note**
>
> The provided column types use cell classes to apply styling. The `CellStyleModule` is required for these types to work correctly.

## Updating Columns

Columns can be controlled by updating the column state, or updating the column definition.

[Column State](https://www.ag-grid.com/javascript-data-grid/column-state/) should be used when restoring a users grid, for example saving and restoring column widths.

Column Definitions should be updated to modify properties that the user cannot control, and as such are not supported by Column State. Whilst column definitions can be used to change stateful properties, this can cause additional side effects.

### Using Column State

The [Grid Api](https://www.ag-grid.com/javascript-data-grid/grid-api/#reference-state-applyColumnState) function `applyColumnState` can be used to update [Column State](https://www.ag-grid.com/javascript-data-grid/column-state/).

```js
// Sort Athlete column ascending
api.applyColumnState({
    state: [
        {
            colId: 'athlete',
            sort: 'asc'
        }
    ]
});
```

In the example below, use the 'Sort Athlete' button to apply a column state.

#### Column State

```ts
import {
  ClientSideRowModelModule,
  ColDef,
  ColumnApiModule,
  ColumnAutoSizeModule,
  GridApi,
  GridOptions,
  ModuleRegistry,
  createGrid,
  enableDevValidations,
} from "ag-grid-community";
import { IOlympicData } from "./interfaces";

// Enable extended validations only for development
if (process.env.NODE_ENV !== "production") {
  enableDevValidations();
}

ModuleRegistry.registerModules([
  ColumnApiModule,
  ColumnAutoSizeModule,
  ClientSideRowModelModule,
]);

const columnDefs: ColDef[] = [
  { field: "athlete" },
  { field: "age" },
  { field: "country" },
  { field: "sport" },
];

let gridApi: GridApi<IOlympicData>;

const gridOptions: GridOptions<IOlympicData> = {
  columnDefs: columnDefs,
  autoSizeStrategy: {
    type: "fitGridWidth",
  },
};

function onBtSortAthlete() {
  gridApi!.applyColumnState({
    state: [{ colId: "athlete", sort: "asc" }],
  });
}

function onBtClearAllSorting() {
  gridApi!.applyColumnState({
    defaultState: { sort: null },
  });
}

const gridDiv = document.querySelector<HTMLElement>("#myGrid")!;
gridApi = createGrid(gridDiv, gridOptions);

fetch("https://www.ag-grid.com/example-assets/small-olympic-winners.json")
  .then((response) => response.json())
  .then((data: IOlympicData[]) => gridApi!.setGridOption("rowData", data));

if (typeof window !== "undefined") {
  // Attach external event handlers to window so they can be called from index.html
  (<any>window).onBtSortAthlete = onBtSortAthlete;
  (<any>window).onBtClearAllSorting = onBtClearAllSorting;
}
```

[Live example: Column State](https://www.ag-grid.com/examples/column-definitions/column-state/typescript)

### Updating Column Definitions

To update an attribute by [Updating Column Definitions](https://www.ag-grid.com/javascript-data-grid/column-updating-definitions/#changing-column-definition), pass a new array of [Column Definitions](https://www.ag-grid.com/javascript-data-grid/column-definitions/) to the grid options.

```
// Define new column definitions
const updatedHeaderColumnDefs = [
  { field: 'athlete', headerName: 'C1' },
  { field: 'age', headerName: 'C2' },
  { field: 'country', headerName: 'C3' },
  { field: 'sport', headerName: 'C4' },
]
// Supply new column definitions to the grid
gridApi.setGridOption('columnDefs', updatedHeaderColumnDefs);
```

In the example below, use the 'Update Header Names' button to update the column definitions.

#### Column Definition Update

```ts
import {
  ClientSideRowModelModule,
  ColDef,
  ColumnAutoSizeModule,
  GridApi,
  GridOptions,
  ModuleRegistry,
  createGrid,
  enableDevValidations,
} from "ag-grid-community";
import { IOlympicData } from "./interfaces";

// Enable extended validations only for development
if (process.env.NODE_ENV !== "production") {
  enableDevValidations();
}

ModuleRegistry.registerModules([
  ColumnAutoSizeModule,
  ClientSideRowModelModule,
]);

const columnDefinitions: ColDef[] = [
  { field: "athlete" },
  { field: "age" },
  { field: "country" },
  { field: "sport" },
];

const updatedHeaderColumnDefs: ColDef[] = [
  { field: "athlete", headerName: "C1" },
  { field: "age", headerName: "C2" },
  { field: "country", headerName: "C3" },
  { field: "sport", headerName: "C4" },
];

let gridApi: GridApi<IOlympicData>;

const gridOptions: GridOptions<IOlympicData> = {
  columnDefs: columnDefinitions,
  autoSizeStrategy: {
    type: "fitGridWidth",
  },
};

function onBtUpdateHeaders() {
  gridApi!.setGridOption("columnDefs", updatedHeaderColumnDefs);
}

function onBtRestoreHeaders() {
  gridApi!.setGridOption("columnDefs", columnDefinitions);
}

const gridDiv = document.querySelector<HTMLElement>("#myGrid")!;
gridApi = createGrid(gridDiv, gridOptions);

fetch("https://www.ag-grid.com/example-assets/small-olympic-winners.json")
  .then((response) => response.json())
  .then((data: IOlympicData[]) => gridApi!.setGridOption("rowData", data));

if (typeof window !== "undefined") {
  // Attach external event handlers to window so they can be called from index.html
  (<any>window).onBtUpdateHeaders = onBtUpdateHeaders;
  (<any>window).onBtRestoreHeaders = onBtRestoreHeaders;
}
```

[Live example: Column Definition Update](https://www.ag-grid.com/examples/column-definitions/column-definition-update/typescript)
