gridcn2.0.0
Column examples

Pinned columns example

Pin columns to the start or end side, resize columns, autosize on double-click, and hide and restore columns.

The demo pins Name to the start side and Score to the end side. Scroll the grid sideways to see them stay in place. Email cannot resize, and the long Bio column fills the free width.

Drag a header's edge to resize (Email is fixed), double-click an edge to autosize the long Bio column, or drag a header to reorder. Hover a header for its menu (or right-click) to pin, hide, or autosize — the toolbar's columns menu restores anything hidden.

Name
Email
Role
Joined
Age
Bio (double-click edge to autosize)
Score
Silent Tiger
user0@example.com
Viewer
2023-07-05
63
Silent Tiger has been a viewer since 2023-07-05.
4
Quick Lion
user1@example.com
Viewer
2021-04-03
67
Quick Lion has been a viewer since 2021-04-03.
89
Swift Wolf
user2@example.com
Manager
2020-10-27
40
Swift Wolf has been a manager since 2020-10-27.
80
Eager Lion
user3@example.com
User
2022-10-08
50
Eager Lion has been a user since 2022-10-08.
51
Eager Tiger
user4@example.com
Viewer
2024-10-07
45
Eager Tiger has been a viewer since 2024-10-07.
83
Silent Wolf
user5@example.com
User
2020-06-01
24
Silent Wolf has been a user since 2020-06-01.
15
Swift Lion
user6@example.com
Viewer
2022-10-18
34
Swift Lion has been a viewer since 2022-10-18.
62
Eager Lion
user7@example.com
Manager
2021-10-07
21
Eager Lion has been a manager since 2021-10-07.
42
Eager Eagle
user8@example.com
Editor
2024-06-11
25
Eager Eagle has been a editor since 2024-06-11.
35
Bold Tiger
user9@example.com
Editor
2024-03-20
19
Bold Tiger has been a editor since 2024-03-20.
52
Bold Bear
user10@example.com
User
2020-04-16
42
Bold Bear has been a user since 2020-04-16.
28
Swift Wolf
user11@example.com
Manager
2022-08-04
47
Swift Wolf has been a manager since 2022-08-04.
9
pnpm dlx shadcn@latest add @gridcn/data-grid-pinning-demo

What the demo shows

  • pin: "start" on Name and pin: "end" on Score pin the columns to the start side and the end side. A pinned column shows a shadow once other columns scroll under it.
  • resizable: false on Email removes its resize handle. Drag the end edge of another header to resize it.
  • On a grid wider than its columns, flex: 1 on Bio fills the free width. Double-click the end edge of its header to autosize it to its content.
  • Hover a header to see the menu button, or right-click the header. The menu pins, unpins, hides, moves, resizes, and autosizes the column. From the keyboard, Shift+Alt+ArrowDown opens the menu of the active cell's column. The demo wires it with DataGridContextMenu and DataGridHeaderDropdown.
  • The DataGridColumnsMenu in the toolbar hides and restores columns.

Pin, resize, and reorder are on for every column. Set pinnable, resizable, or reorderable to false on a column to turn one off. Set enableColumnPinning, enableColumnResize, or enableColumnReorder to false to turn one off for the grid. To pin rows as well, see the pinned rows example.

The column options of the demo, shortened:

import { defineColumns } from "@/components/data-grid/data-grid";

type Person = {
  id: string;
  name: string;
  email: string;
  bio: string;
  score: number;
};

export const columns = defineColumns<Person>()([
  { id: "name", header: "Name", accessorKey: "name", width: 160, pin: "start" },
  {
    id: "email",
    header: "Email",
    accessorKey: "email",
    width: 200,
    resizable: false,
  },
  { id: "bio", header: "Bio", accessorKey: "bio", width: 140, flex: 1 },
  {
    id: "score",
    header: "Score",
    accessorKey: "score",
    type: "number",
    width: 90,
    pin: "end",
  },
]);

On this page