diff --git a/packages/grid/x-data-grid-pro/src/tests/rowPinning.DataGridPro.test.tsx b/packages/grid/x-data-grid-pro/src/tests/rowPinning.DataGridPro.test.tsx
index bde6342f1aa1..dfbaee922b0a 100644
--- a/packages/grid/x-data-grid-pro/src/tests/rowPinning.DataGridPro.test.tsx
+++ b/packages/grid/x-data-grid-pro/src/tests/rowPinning.DataGridPro.test.tsx
@@ -765,4 +765,15 @@ describe(' - Row pinning', () => {
expect(screen.getByRole('grid').getAttribute('aria-rowcount')).to.equal(`${rowCount + 1}`); // +1 for header row
});
+
+ // https://github.com/mui/mui-x/issues/5845
+ it('should work with `getCellClassName` when `rows=[]`', () => {
+ const className = 'test-class-name';
+ render(
+ className} />,
+ );
+
+ expect(getRowById(0)!.classList.contains(className)).to.equal(true);
+ expect(getRowById(1)!.classList.contains(className)).to.equal(true);
+ });
});
diff --git a/packages/grid/x-data-grid/src/components/GridRow.tsx b/packages/grid/x-data-grid/src/components/GridRow.tsx
index f570c14f170c..29a591c5a4ee 100644
--- a/packages/grid/x-data-grid/src/components/GridRow.tsx
+++ b/packages/grid/x-data-grid/src/components/GridRow.tsx
@@ -272,7 +272,7 @@ function GridRow(props: React.HTMLAttributes & GridRowProps) {
let rowClassName: string | null = null;
if (typeof rootProps.getRowClassName === 'function') {
- const indexRelativeToCurrentPage = index - currentPage.range!.firstRowIndex;
+ const indexRelativeToCurrentPage = index - (currentPage.range?.firstRowIndex || 0);
const rowParams: GridRowClassNameParams = {
...apiRef.current.getRowParams(rowId),
isFirstVisible: indexRelativeToCurrentPage === 0,