Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

226
Vistas
How can I edit a cell of a DataGrid by code?

I would like to edit a cell in a Datagrid by code.

The Datagrid itself has editable columns (firstName and lastName), but if you regret this change there is a column with a cancel button per row that should reset the cells of the same row to the default values.

You can make changes in several rows of the datagrid and just restore some rows, not all of them, so reseting the entire datagrid isn't an option.

How should I approach this?

Thank you in advance!

import React from "react";
import { DataGrid } from "@mui/x-data-grid";
import { Button } from "@mui/material";

export default function DataTable() {

    const columns = [
        { field: 'id', headerName: 'ID' },
        { field: 'firstName', headerName: 'First name', editable: true },
        { field: 'lastName', headerName: 'Last name', editable: true },
        { field: 'cancel', headerName: 'Cancel', renderCell: (params) => <button onClick={ handleButton }/> },
    ];

    const rows = [
        { id: 1, lastName: 'Snow', firstName: 'Jon' },
        { id: 2, lastName: 'Lannister', firstName: 'Cersei' },
        { id: 3, lastName: 'Lannister', firstName: 'Jaime' },
        { id: 4, lastName: 'Stark', firstName: 'Arya' },
        { id: 5, lastName: 'Targaryen', firstName: 'Daenerys' },
    ];

    const handleButton = (event) => {
        // CODE HERE
    }

    return (
        <DataGrid
            rows = {rows}
            columns = { columns }
        />
    );
}
about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can use useState hook to update the table data with onCellEditCommit (is to sync data with single-cell update)and handleButton(is to reset the row) handlers defined as below.

Try like below:

import React, { useState } from "react";
import { DataGrid } from "@mui/x-data-grid";

export default function DataTable() {
  const columns = [
    { field: "id", headerName: "ID" },
    {
      field: "firstName",
      headerName: "First name",
      editable: true
    },
    { field: "lastName", headerName: "Last name", editable: true },
    {
      field: "cancel",
      headerName: "Cancel",
      renderCell: (params) => (
        <button onClick={() => handleButton(params)}>cancel</button>
      )
    }
  ];

  const rows = [
    { id: 1, lastName: "Snow", firstName: "Jon" },
    { id: 2, lastName: "Lannister", firstName: "Cersei" },
    { id: 3, lastName: "Lannister", firstName: "Jaime" },
    { id: 4, lastName: "Stark", firstName: "Arya" },
    { id: 5, lastName: "Targaryen", firstName: "Daenerys" }
  ];

  const [data, setData] = useState(rows);

  const handleButton = ({ id }) => {
    setData((prevData) =>
      prevData.map((item) =>
        item.id === id ? { ...item, firstName: "", lastName: "" } : item
      )
    );
  };

  const onCellEditCommit = ({ id, field, value }) => {
    setData((prevData) =>
      prevData.map((item) =>
        item.id === id ? { ...item, [field]: value } : item
      )
    );
  };

  console.log(data);

  return (
    <div style={{ height: 400, width: "100%" }}>
      <DataGrid
        rows={data}
        columns={columns}
        onCellEditCommit={onCellEditCommit}
      />
    </div>
  );
}

Working Demo

Edit mui/x-data-grid edit colums with useState

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda