I have created custom collection for my data and now I want to display the data in the admin grid using collection.Can you please help me out ? Here is my code.

      $connection = Mage::getSingleton('core/resource')->getConnection('core_read');
      $sql = "Select * from TABLE_NAME";
      $rows = $connection->fetchAll($sql);
      $data = new Varien_Object();

      $collection_of_things = new Varien_Data_Collection();
      return parent::_prepareCollection();

I am getting the blank admin grid for it.

Here is my _prepareColumns function

protected function _prepareColumns() {

      $this->addColumn('Field 1', array(
          'header'    => 'ID',
          'align'     =>'center',
          'width'     => '10px',
          'index'     => 'field_1'
      $this->addColumn('field_2', array(
          'header'    => 'Field 2',
          'align'     =>'left',
          'width'     => '50px',
          'index'     => 'field_2'
      $this->addColumn('field_3', array(
          'header'    => 'Field 3',
          'align'     =>'left',
          'width'     => '50px',
          'index'     => 'field_3'
      $this->addColumn('field_4', array(
          'header'    => 'Field 4',
          'align'     =>'left',
          'width'     => '50px',
          'index'     => 'field_4',
          'filter_index' => 'field_4',
          'type' => 'datetime'
      $this->addColumn('field_5', array(
          'header'    => 'Field 5',
          'align'     =>'center',
          'width'     => '50px',
          'index'     => null,
          'renderer'      => 'Namespace_MyModule_Block_Adminhtml_Template_Grid_Renderer_Image',
          'sortable'    => false,
          'filter' => false,
          'is_system'   => true
      $this->addExportType('*/*/exportCsv', 'CSV');
      return parent::_prepareColumns();
  • Do you define function _prepareColumns() in grid? if yes show code. – Jaydeep Sinh Parmar Jan 23 '15 at 12:02

This is not how you populate a collection. It might work if you actually add each row from your query result as item:

  $collection_of_things = new Varien_Data_Collection();
  foreach ($rows as $row) {
      $collection_of_things->addItem(new Varien_Object($row));

But grids are designed to work with Varien_Data_Db_Collections that were loaded directly from database (and allow filter functionality etc.), so you should follow the Magento practice of defining model, resource model and collection for your custom table and not using plain SQL anywhere.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.