Magento: Add new column to the end or after any column of admin grid

This article shows, how you can add a new column at the end of the admin grid or after any column of the admin grid in Magento 1.x.

Suppose, you have a custom module and you need to add a column to product grid or order grid, etc. For this, you need to rewrite that particular grid block with your custom module’s block grid class.

In your custom block grid class, you have _prepareColumns() function where you specify the columns you need to show in the grid.

Here, I will be showing how you can add a column to an existing grid without copying all the columns adding code from parent grid class.

Add a column to the end of the grid

Suppose, the column you need to add is named myNewColumn. We can just call the parent _prepareColumns() function and then add the new column and return it.


protected function _prepareColumns()
{   
    parent::_prepareColumns();
    $this->addColumn('myNewColumn', array(
        'header' => Mage::helper('catalog')->__('My New Column'),
        'index'  => 'myNewColumn',
        'width'  => '50',       
    ));
    return $this;   
}

Add a column after any particular grid column

Suppose, the column you need to add is named myNewColumn. And, you need to add this column after a particular column of the existing grid. Suppose, that column name is xyz. Now, you need to add your new column after column ‘xyz’. This can be done using addColumnAfter() function.


protected function _prepareColumns()
{   
    $this->addColumnAfter('myNewColumn', array(
        'header' => Mage::helper('catalog')->__('My New Column'),
        'index'  => 'myNewColumn',
        'width'  => '50',       
    ), 'xyz');
    return parent::_prepareColumns();
}

Hope this helps. Thanks.