class My_Grid extends Mage_Adminhtml_Block_Widget_Grid { protected function _prepareCollection() { // Your code to fetch data and create a collection } protected function _prepareColumns() { // Your code to define the columns } } $grid = new My_Grid(); echo $grid->toHtml();
class My_Grid extends Mage_Adminhtml_Block_Widget_Grid { protected function _prepareCollection() { // Your code to fetch data and create a collection $this->setDefaultSort('my_column'); $this->setDefaultDir('ASC'); $this->setFilterVisibility(true); } protected function _prepareColumns() { // Your code to define the columns $this->addColumn('my_column', array( 'header' => 'My Column', 'index' => 'my_column' )); } } $grid = new My_Grid(); echo $grid->toHtml();This example is similar to the previous one, but we have added options to enable sorting and filtering for the grid. The setDefaultSort and setDefaultDir methods set the default sorting order for the grid, and the setFilterVisibility method enables the use of filters. In the _prepareColumns method, we define a column ('my_column') which will be sortable and displayed in the grid. This example demonstrates the use of several convenient methods available in the Mage_Adminhtml_Block_Widget_Grid class.