Add a helper entity(non persistent) which will contain a boolean attribute and an association to your datagrid entity. On click of the image/icon change the value of the attribute. Use this attribute to set the visibility of your datagrid.
Optionally you could also use ShowByCondition widget but that would require you to add a css class. You can find more in their documentation on github: https://marketplace.mendix.com/link/component/2539
Hope this helps!