gridcn2.0.0
Basic examples

Minimal grid example

Render a grid from one registry item, three columns, and seed rows, with no state of your own.

The smallest working grid needs the data-grid item, a column list, and defaultData. The grid seeds its rows once from defaultData, then owns them and applies every edit itself.

Name
Age
Active
Ada Lovelace
28
Grace Hopper
34
Margaret Hamilton
31
pnpm dlx shadcn@latest add @gridcn/data-grid-minimal-demo

What the demo shows

  • DataGrid composes the provider, the scroll root, the header, and the body.
  • defineColumns types three columns: text, number, and checkbox.
  • defaultData seeds the rows. The grid ignores later changes to the prop.
  • className sizes the grid. The demo sets w-full h-[240px]. The grid needs a bounded height to virtualize. Without one, it draws at most 200 rows.

The grid reads the row id from the id field of each row. If a row has no string or number id field, the grid throws. Pass getRowId to read the id from another field. If the app must own the rows, pass data and onDataChange.

A minimal version of the demo, with its imports:

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

type Person = { id: string; name: string; age: number; active: boolean };

const columns = defineColumns<Person>()([
  { id: "name", header: "Name", accessorKey: "name", type: "text" },
  { id: "age", header: "Age", accessorKey: "age", type: "number" },
  { id: "active", header: "Active", accessorKey: "active", type: "checkbox" },
]);

const rows: Person[] = [
  { id: "1", name: "Ada Lovelace", age: 28, active: true },
  { id: "2", name: "Grace Hopper", age: 34, active: true },
];

export function PeopleGrid() {
  return <DataGrid defaultData={rows} columns={columns} className="h-60" />;
}

On this page