title: Magento grid column sorting
body: <p>Once upon a time I tried creating some custom columns.  I created the entire XML structure like I should have.  I created controllers.  I even created the custom grid controller.  </p>
  
  <p>After creating my custom grid, I figured the columns would sort.  I was wrong, dead wrong.  Clicking on the column headers do nothing.  </p>
  
  <p>Suggestions?</p>
  
  <pre><code>class Company_Googlemerchant_Block_Adminhtml_Products_Grid extends Mage_Adminhtml_Block_Widget_Grid
  {
  public function __construct()
  {
      parent::__construct();
      $this-&gt;setId('gm_product_grid');
      $this-&gt;setDefaultSort('id');
      $this-&gt;setDefaultDir('ASC');
      $this-&gt;setSaveParametersInSession(false);
  }
  
  protected function _prepareCollection()
  {
      $storeId = 1;
      $collection = Mage::getModel('catalog/product')-&gt;getCollection()-&gt;addStoreFilter($storeId);
      $collection
          -&gt;addAttributeToSelect('enable_googlemerchant')
          -&gt;addAttributeToSelect('name')
          -&gt;addAttributeToSelect('entity_id')
          -&gt;addAttributeToSelect('type_id')
          -&gt;addAttributeToSelect('status')
          -&gt;addFieldToFilter('enable_googlemerchant', array( "eq" =&gt; '1') )
          -&gt;addFieldToFilter('status', array( "eq" =&gt; '1') )
          -&gt;addAttributeToSort('name', 'asc')
          ;
  
      $this-&gt;setCollection($collection);
  
      return parent::_prepareCollection();
  }
  
  protected function _prepareColumns()
  {
      $this-&gt;addColumn('id', array(
        'header'    =&gt; Mage::helper('googlemerchant')-&gt;__('ID'),
        'align'     =&gt;'left',
        'index'     =&gt; 'entity_id',
        'width'     =&gt; '100px',
      ));
  
      $this-&gt;addColumn('product_name', array(
        'header'    =&gt; Mage::helper('googlemerchant')-&gt;__('Product Name'),
        'align'     =&gt;'left',
        'index'     =&gt; 'name',
        'width'     =&gt; '250px',
      ));
  
      $this-&gt;addColumn('type_id', array(
        'header'    =&gt; Mage::helper('googlemerchant')-&gt;__('Product Type'),
        'align'     =&gt;'left',
        'index'     =&gt; 'type_id',
        'width'     =&gt; '100px',
      ));
  
      $this-&gt;addColumn('action', array(
          'header'  =&gt; Mage::helper('googlemerchant')-&gt;__('Action'),
          'width'   =&gt; '100px',
          'type'    =&gt; 'action',
          'getter'  =&gt; 'getId',
          'actions' =&gt; array(
              array(
                  'caption' =&gt; Mage::helper('googlemerchant')-&gt;__('Remove from export'),
                  'url'     =&gt; array( 'base'   =&gt; '*/*/removeexport' ),
                  'field'   =&gt; 'id'
              )
          ),
          'filter'    =&gt; false,
          'sortable'  =&gt; true,
          'index'     =&gt; 'id',
      ));
  
      return parent::_prepareColumns();
  }
  </code></pre>
  
  <p>}</p>