Magento 2.1 – How to Add Custom Button on Custom Grid in Customer View Adminhtml

adminhtmlgrid layoutmagento-2.1

I have created a custom grid on customer view in admin

 protected function _construct()
    {
        parent::_construct();

        $this->setDefaultSort('model','desc');
        $this->setSortable(true);
        $this->setPagerVisibility(true);
        $this->setFilterVisibility(true);
    }

are used to get sorter , pager and filters

How can i add new custom button here above the grid?

enter image description here

Best Answer

In your grid.php (where grid columns and sorter etc)

 public function getMainButtonsHtml()
    {
        $html = parent::getMainButtonsHtml();//get the parent class buttons
        $addButton = $this->getLayout()->createBlock('Magento\Backend\Block\Widget\Button')
            ->setData(array(
            'label'     => 'Add',
            'onclick'   => "setLocation('#')",
            'class'   => 'task'
        ))->toHtml();
        return $addButton.$html;
    }

SOURCE